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?
Related
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)))
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 need to create a file and write its header contents before the for loop. If I do it inside the loop, header content repeats for every iteration of the loop. When I execute the below code, I get the error "I/O operation on a closed file".
Is wondering there a work around for this issue? Any suggestions would be appreciated. TIA !!
csv_filename = "testfile4.csv"
with open(csv_filename, "wt") as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
# CSV Header
header = ["Post ID", "Permalink", "Create Time"]
writer.writerow(header)
for group in getGroups():
feed = getValues(group["id"], group["name"])
with open(csv_filename, "a") as csvfile:
for item in feed:
row = [item["id"], item["permalink_url"], item["created_time"]]
writer.writerow(row)
You could process all necessary operations in single with statement
csv_filename = "testfile4.csv"
with open(csv_filename, "wt") as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
header = ["Post ID", "Permalink", "Create Time"]
writer.writerow(header)
for group in getGroups():
feed = getValues(group["id"], group["name"])
for item in feed:
row = [item["id"], item["permalink_url"], item["created_time"]]
writer.writerow(row)
This question already has answers here:
CSV file written with Python has blank lines between each row
(11 answers)
Closed 7 years ago.
I have a code and a when i run it it adds information to csv file but every time it adds something new it skips a line. How to stop it?
import csv
if group == '1':
with open('class1.csv', 'a') as csvfile:
fieldnames = ['Forename', 'Surname', 'Score']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({'Forename': name, 'Surname': name1, 'Score':score})
elif group == '2':
with open('group1.csv', 'a') as csvfile:
fieldnames = ['Forename', 'Surname', 'Score']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({'Forename': name, 'Surname': name1, 'Score':score})
elif group == '3':
with open('class3.csv', 'a') as csvfile:
fieldnames = ['Forename', 'Surname', 'Score']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({'Forename': name, 'Surname': name1, 'Score':score})
I think you missed writer.writeheader in every if and elif conditions
if group == '1':
with open('class1.csv', 'a') as csvfile:
fieldnames = ['Forename', 'Surname', 'Score']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader() #you missed it
writer.writerow({'Forename': 'name', 'Surname': 'name1', 'Score':'score'})
#rest of code
(Assuming you're in Python 3), each time you call:
with open('mycsv.csv', 'a') as csvfile:
if you're on a Windows machine, try:
with open('mycsv.csv', 'a', newline='') as csvfile:
Edit: explanation / clarification
Per Python's documentation on csv writer,
If csvfile is a file object [meaning, if the file you passed to csv writer is called csvfile], it should be opened with newline='' ...
If newline='' is not specified, newlines embedded inside quoted fields
will not be interpreted correctly, and on platforms [such as Windows]
that use \r\n linendings on write an extra \r will be added. It should
always be safe to specify newline='', since the csv module does its
own (universal) newline handling.
As martineau pointed out, you can avoid this issue in Python 2 by writing to the file in byte mode ('ab', or 'wb'). Python 3's csv module likes to work with strings, not bytes. Therefore, it should be opened in text mode (either 'w', or 'a').
EDIT: Thanks for the answers guys, got what I needed!!
Basically I am trying to take what I have stored in my textfile and I am trying to write that into a .csv file. In my file are tweets that I have stored and I am trying to have one tweet in each cell in my .csv file.
Right now it is only taking one tweet and creating a .csv file with it and I need it to take all of them. Any help is greatly appreciated. Here is what I have so far.
with open('reddit.txt', 'rb') as f:
reader = csv.reader(f, delimiter=':', quoting = csv.QUOTE_NONE)
for row in reader:
print row
cr = csv.writer(open('reddit.csv', 'wb'))
cr.writerow(row)
You'll need to create the writer outside of the loop:
with open('reddit.txt', 'rb') as input_file:
reader = csv.reader(input_file, delimiter=':', quoting = csv.QUOTE_NONE)
with open('reddit.csv', 'wb') as output_file:
writer = csv.writer(output_file)
for row in reader:
writer.writerow(row)
Although here it might be cleaner to open the files without with:
input_file = open('reddit.txt', 'rb')
output_file = open('reddit.csv', 'wb')
reader = csv.reader(input_file, delimiter=':', quoting=csv.QUOTE_NONE)
writer = csv.writer(output_file)
for row in reader:
writer.writerow(row)
input_file.close()
output_file.close()
Or you can still use with and just have a really long line:
with open('reddit.txt', 'rb') as input_file, open('reddit.csv', 'wb') as output_file:
reader = csv.reader(input_file, delimiter=':', quoting = csv.QUOTE_NONE)
writer = csv.writer(output_file)
for row in reader:
writer.writerow(row)
The line cr = csv.writer(open('reddit.csv', 'wb')) is inside the for loop. You need to open the file just once, place this line after
reader = csv.reader(f, delimiter=':', quoting = csv.QUOTE_NONE)
Then write to it as you did in each loop iteration.