I am trying to loop multiple arrays and write them into csv file with under right filednames.
Here is the working code but is is bad approach and not professional:
# zip arrays
rows = zip(en,pl,tr,de)
# Write to CSV
with open('translations.csv', mode='w', newline='', encoding="utf-8") as csv_file:
fieldnames = ['English', 'Polish', 'Turkish', 'German']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
info = inspect.getmembers(row)
english = row[0]
polish = row[1]
turkish = row[2]
german = row[3]
writer.writerow({'English': english, 'Polish':polish, 'Turkish':turkish, 'German':german})
I tried to change the code and it is really messed up:
# zip arrays
rows = zip(en,pl,tr,de)
# Write to CSV
with open('translations.csv', mode='w', newline='', encoding="utf-8") as csv_file:
fieldnames = ['English', 'Polish', 'Turkish', 'German']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
for i in range(len(fieldnames)):
writer.writerow({fieldnames[i]:row[i]})
So every translation should under right filedname. Here the result of this code:
English,Polish,Turkish,German
Test Name,,,
,Test Name,,
,,Test Name,
,,,Test Name
Nazwa testu,,,
,Nazwa testu,,
,,Nazwa testu,
,,,Nazwa testu
test Adı,,,
,test Adı,,
,,test Adı,
,,,test Adı
Testname,,,
,Testname,,
,,Testname,
,,,Testname
It is really messed up. Please help me to solve this problem. Thank you!
You probably need this. Instead of
for i in range(len(fieldnames)):
writer.writerow({fieldnames[i]:row[i]})
Use:
writer.writerow(dict(zip(fieldnames, row)))
Related
I am trying to make a function that takes a threshold and determines which names from a csv file of song names and their lyrics that contain human names and the function
should create a csv file named outputfile that contains the number of distinct names, the name of
the song and the artist.
import csv
def findName(thresh, outputFile):
dictNames={}
with open('allNames.csv') as csvfile:
reader = csv.DictReader(csvfile, delimiter="\t")
for row in reader:
if row["name"] in dictNames:
dictNames[row["name"]] +=1
else:
dictNames[row["name"]]=1
with open(outputFile, "w", newline='') as outfile:
headers= ["song", "artist", "year"]
writer=csv.DictWriter(outfile, fieldnames=headers)
writer.writeheader()
for key, val in dictNames.items():
if val>= thresh:
writer.writerow({key: val})
csvfile.close()
outfile.close()
What's the rationale for not using Pandas here?
Not sure I fully understand your question, but I'm thinking something like:
df = pd.read_csv('allNames.csv')
#partition df after threshold
df['index'] = df.index
def partition_return(threshold, df):
df = df.loc[df['index'] >= threshold].reset_index(drop=true)
df = df[['song', 'artist', 'year]]
df['count_names_dist'] = len(df['artist'].unique())
df.to_csv('outfile.csv', index=False)
I have a csv file with three lines and three columns here.
This is the csv file
At first I want to print all the lines.
Subsequently, for each of them, program check whether it is written in the second field(index 1) USA. If so, program will take the price from the third field and multiply it by two.
Now I need to rewrite this doubled price instead of 2000 (in line with the USA)
import csv
with open('countries.csv', 'r') as source:
reader = csv.reader(source)
writer = csv.writer(source)
for line in reader:
print(*line, sep=';')
with open('countries.csv', 'r') as source:
reader = csv.reader(source)
for line in reader:
if line[2] == "USA":
actual_price = int(line[2])
print(actual_price)
new_price = int(actual_price) * 2
print(new_price)
Someone has already advised me to use the creation of a new file.
But this causes problems when I want to work with the data in the file first.
import csv
import os
with open('countries.csv', mode='r') as oldfile, open(
'countries.tmp', mode='w', newline='') as newfile:
# define a reader and a writer
reader = csv.reader(oldfile, delimiter=';', quotechar='"')
writer = csv.writer(newfile, delimiter=';', quotechar='"',
quoting=csv.QUOTE_MINIMAL)
for line in reader:
print(*line, sep=';')
# copy everything changing the third field
for line in reader:
if line[2] == "USA":
actual_price = int(line[2])
print(actual_price)
new_price = int(actual_price) * 2
print(new_price)
for row in reader:
writer.writerow([row[0], row[1], ,new_price])
# ok, time to rename the file
os.replace('countries.tmp', 'countries.csv')
Thank you for answer
You are changing new_price at every iteration of your for loop. You should therefore be writing the row within the loop where you change the value:
with open('countries.csv', mode='r') as oldfile, open('countries.tmp', mode='w', newline='') as newfile:
reader = csv.reader(oldfile, delimiter=';', quotechar='"')
writer = csv.writer(newfile, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for row in reader:
price = int(row[2])
if row[1] == "USA":
price = price*2
writer.writerow([row[0], row[1], price])
os.replace('countries.tmp', 'countries.csv')
This question was answered/resolved in the comments section.
I am a noob to Python and I wrote the code below thinking it would copy all the rows with "NY" as the state in the state field/column to a new csv file called "Output.csv".
import csv
f = open(r'C:\Users..\input.csv', 'r')
reader = csv.DictReader(f, delimiter=',')
output = open("C:...\Output.csv",'w')
fieldnames = ['firstScan', 'FinalScan', 'City', 'State', 'cld', 'daydiff']
writer = csv.DictWriter(output, fieldnames=fieldnames, delimiter=',')
for row in reader:
if row['State'] == 'NY':
writer.writerow(row)
Everything runs fine but the output csv is completely blank. The first tab is named "Output" but the sheet is blank. If I have it output to txt, that is blank as well. Any suggestions?
Try this instead:
import csv
with open('C:/Users/felasniper/Desktop/input.csv') as f:
reader = csv.DictReader(f, delimiter=',')
output = open("C:/Users/felasniper/Desktop/Output.csv", 'w')
fieldnames = ['firstScan', 'FinalScan', 'City', 'State', 'cld', 'daydiff']
writer = csv.DictWriter(output, fieldnames=fieldnames, delimiter=',')
for row in reader:
if row['State'] == 'NY':
writer.writerow(row)
I have a csv file, l__cyc.csv, that contains this:
trip_id, time, O_lat, O_lng, D_lat, D_lng
130041910101,1300,51.5841153671,0.134444590094,51.5718053872,0.134878021928
130041910102,1335,51.5718053872,0.134878021928,51.5786920389,0.180940040247
130041910103,1600,51.5786920389,0.180940040247,51.5841153671,0.134444590094
130043110201,1500,51.5712712038,0.138532882664,51.5334949484,0.130489470325
130043110202,1730,51.5334949484,0.130489470325,51.5712712038,0.138532882664
And I am trying to pull out separate values, using:
with open('./l__cyc.csv', 'rU') as csvfile:
reader = csv.DictReader(csvfile)
origincoords = ['{O_lat},{O_lng}'.format(**row) for row in reader]
with open('./l__cyc.csv', 'rU') as csvfile:
reader = csv.DictReader(csvfile)
trip_id = ['{trip_id}'.format(**row) for row in reader]
with open('./l__cyc.csv', 'rU') as csvfile:
reader = csv.DictReader(csvfile)
destinationcoords = ['{D_lat},{D_lng}'.format(**row) for row in reader]
Where origincoords should be 51.5841153671, 0.134444590094,
trip_id should be 130041910101, and destinationcoords should be
51.5718053872, 0.134878021928.
However, I get a KeyError:
KeyError: 'O_lat'
Is this something simple and there's something fundamental I'm misunderstanding?
You just avoid the space between headers
trip_id,time,O_lat,O_lng,D_lat,D_lng
OR
reader = csv.DictReader(csvfile, skipinitialspace=True)
First things first, you get the key error, because the key does not exist in your dictionary.
Next, I would advise against running through the file 3 times, when you can do it a single time!
For me it worked, when I added the fieldnames to the reader.
import csv
from cStringIO import StringIO
src = """trip_id, time, O_lat, O_lng, D_lat, D_lng
130041910101,1300,51.5841153671,0.134444590094,51.5718053872,0.134878021928
130041910102,1335,51.5718053872,0.134878021928,51.5786920389,0.180940040247
130041910103,1600,51.5786920389,0.180940040247,51.5841153671,0.134444590094
130043110201,1500,51.5712712038,0.138532882664,51.5334949484,0.130489470325
130043110202,1730,51.5334949484,0.130489470325,51.5712712038,0.138532882664
"""
f = StringIO(src)
# determine the fieldnames
fieldnames= "trip_id,time,O_lat,O_lng,D_lat,D_lng".split(",")
# read the file
reader = csv.DictReader(f, fieldnames=fieldnames)
# storage
origincoords = []
trip_id = []
destinationcoords = []
# iterate the rows
for row in reader:
origincoords.append('{O_lat},{O_lng}'.format(**row))
trip_id.append('{trip_id}'.format(**row))
destinationcoords.append('{D_lat},{D_lng}'.format(**row))
# pop the header off the list
origincoords.pop(0)
trip_id.pop(0)
destinationcoords.pop(0)
# show the result
print origincoords
print trip_id
print destinationcoords
I don't really know what you are trying to achieve there, but I'm sure there is a better way of doing it!
I am getting a keyerror when trying to read data from a csv. Below is the code that writes to the csv, followed by the code that reads from it. Python 2.7 on Ubuntu. Any help is appreciated!
#setting up the csv
with open(databaseLocal, 'wb') as csvfile:
fieldnames = ['cpu', 'pid', 'memory']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames,
quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writeheader()
#Here is one spot where it's written to...
if data[:6] == 'active':
print 'mem info received'
memInfo = data[7:-1]
writer.writerow({'cpu': '_', 'pid': pidValue, 'memory': memInfo})
#and here is where it's read from, there is a KeyError for the line
#where we try to get row['pid]
with open(d, 'rb') as csvfile:
reader = csv.DictReader(csvfile, delimiter=' ', quotechar='|')
for row in reader:
print row['pid']
print row.keys()
When I just print 'row.keys()' all three of the keys show up. Not sure why I can't access 'row['pid'] or any of the others.
Thanks!
Matt
You are writing to databaseLocal, but trying to read from d. Are you sure they are referencing the same file?