I have the following code that works fine:
with open('userWithAgentProp.csv','w+') as f:
w = csv.DictWriter(f,user_keys)
w.writeheader()
for user in userAgentProp_list:
w.writerow(user)
I used this code to write a function:
def createCSVOutput(fileName, keys, listOfLists):
with open(fileName, 'w+') as f:
w= csv.dictWriter(f, keys)
w.writeheader()
for row in listOfLists:
w.writerow(row)
When I call the function:
createCSVOutput('new_test_csv.csv', user_keys, userAgentProp_list)
I get the following error:
File "mongodb_script_2.py", line 101, in <module>
createCSVOutput('new_test_csv.csv', user_keys, userAgentProp_list)
File "mongodb_script_2.py", line 54, in createCSVOutput
w= csv.dictWriter(f, keys)
AttributeError: 'module' object has no attribute 'dictWriter'
Why does it work for user_keys variable in the script, but not in the function?
You have a typo:
csv.dictWriter is wrong. It should be csv.DictWriter.
Related
I am trying to do a relatively simple parse of a csv file, and I don't understand why the csv module is not working. Here is my code:
import csv
def getFromCSV(fileName):
with open(fileName, 'r') as f:
reader = csv.reader(f)
data = list(reader)
return data
def append_row(fileName, my_list):
with open(fileName, 'a') as output:
writer = csv.writer(output)
writer.writerow(my_list)
data = getFromCSV('dh_internal_all.csv')
for row in data:
if '25252' not in row:
print(row)
append_row('parsed.csv',[row])
This returns:
dh-dfbhv2l:Documents jwr38$ python3 remove_bad_data.py
Traceback (most recent call last):
File "remove_bad_data.py", line 13, in <module>
data = getFromCSV('dh_internal_all.csv')
File "remove_bad_data.py", line 3, in getFromCSV
reader = csv.reader(f)
NameError: name 'csv' is not defined
Thank you in advance for any tips.
EDIT: when I run python3 in terminal, then import csv, and then csv, it seems to recognize it, it returns:
<module 'csv' from '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/csv.py'>
You pasted the wrong code. In your traceback, the faulting line is 3, but in this code, it's 5 - the two missing lines are probably the "import csv" lines.
Trying to read a csv into a list.
Code is as follows:
import csv
with open('file.csv','r') as fin:
reader = csv.reader(fin)
mylist = list(reader)
print (mylist)
def gen_list():
with open('file.csv','r') as fin:
reader = csv.reader(fin)
mylist = list(reader)
print (mylist)
def main():
gen_list()
return 0
My output is:
[['one','two','three']]
Traceback():
....
line 11, in gen_list
reader = csv.reader(fin)
AttributeError: 'str' object has no attribute 'reader'
I can prevent this error occurring by putting 'import csv' as the first line of the gen_list function.
why does the code not work when placed in a function?
Turns out I was using csv in another function by accident. Once I changed csv the issue went away
Function:
def get_list(csv):
with open(csv,'rt') as fin:
for line in fin:
drList.append(gen_list(line))
return (drList)
Try this:
import csv
with open('file.csv','r') as fin:
reader = csv.reader(fin)
mylist = list(reader)
print (mylist)
def gen_list():
with open('file.csv','r') as fin:
reader = csv.reader(fin)
mylist = list(reader)
print (mylist)
def main():
gen_list()
return 0
It seems that the indentation for reader within the open within gen_list is wrong
Hi I'm trying to open simple csv file with the header from an external file:
got next file named: name.csv with next content:
Leo,Days,Ju
Tomas,Lee,Bruce
Max,Perez,Smith
If I code:
import csv
sep = ','
with open('name.csv') as csvfile:
fieldnames = ['name', 'paterno', 'materno']
reader = csv.DictReader(csvfile,fieldnames)
for row in reader:
list = (row['name'], \
row['materno'])
print (sep.join(list))
The result is desired like:
Leo,Ju
Tomas,Bruce
Max,Smith
But if got an extra file with headers named hdr_name.txt with:
['name', 'paterno', 'materno']
With this new code:
import csv
sep = ','
fieldnames = open('hdr_name.txt', 'r').read()
with open('name.csv') as csvfile:
print(fieldnames)
reader = csv.DictReader(csvfile,fieldnames)
for row in reader:
list = (row['name'], \
row['materno'])
print (sep.join(list))
Got as result:
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
KeyError: 'name'
But if I ask for 'name' in fieldnames, is there!
>>> 'name' in fieldnames
True
>>>
What I'm doing wrong, with opening header from external file ?
fieldnames is a string that looks like this:
"['name', 'paterno', 'materno']"
Naturally, a membership test will return true, but that does not imply fieldnames is a list. Remember, file.read returns a string - you still need to cast it to a list.
This doesn't appear to look like JSON, so I'd recommend ast:
import ast
with open('hdr_name.txt', 'r') as f:
fieldnames = ast.literal_eval(f.read().strip())
I am very new to python (and programming in general). The code pasted below is for editing an existing csv. But I am getting an error.
Error is
Traceback (most recent call last):
File "C:\Users\Samsung Pc\Desktop\hungrypy1\hungry_data.py", line 34, in <module>
writer.writeheader()
File "C:\Users\Samsung Pc\AppData\Local\Programs\Python\Python36-32\lib\csv.py", line 144, in writeheader
self.writerow(header)
File "C:\Users\Samsung Pc\AppData\Local\Programs\Python\Python36-32\lib\csv.py", line 155, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "C:\Users\Samsung Pc\AppData\Local\Programs\Python\Python36-32\lib\tempfile.py", line 483, in func_wrapper
return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'
[Finished in 0.5s]
I have tried to look for the answer and it seems to be a type mismatch from binary to string. I have tried to open the file as "r" instead of "rb" however is the same error. Please help. The code is as below.
import csv
import shutil
from tempfile import NamedTemporaryFile
def get_length(file_path):
with open("data.csv", "r") as csvfile:
reader = csv.reader(csvfile)
reader_list = list(reader)
print(reader_list)
return len(reader_list)
def append_data(file_path, name, email):
fieldnames = ['id', 'name', 'email'
next_id = get_length(file_path)
with open(file_path, "a", newline = '') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({
"id": next_id,
"name": name,
"email": email,
})
append_data("data.csv", "Niraj", "test#test.com")
filename = "data.csv"
temp_file = NamedTemporaryFile(delete = False)
with open(filename, "r") as csvfile, temp_file:
reader = csv.DictReader(csvfile)
fieldnames = ['id','name','email','amount','sent']
writer = csv.DictWriter(temp_file, fieldnames = fieldnames)
writer.writeheader()
for row in reader:
writer.writerow({
"id" : row["id"],
"name" : row["name"],
"email": row["email"],
"amount": "1234.34",
"sent": "",
})
#shutil.move(temp_file.name. filename)
You should not read and write to the same file at the same time:
with open(filename, "r") as csvfile, temp_file:
Even though you give this file two names, they are point to the same file in you disk, changeh it to:
with open(filename, "r") as csvfile, open('temp_file.csv', 'w') as temp_file:
what you're doing:
temp_file = NamedTemporaryFile(delete = False)
with open(filename, "r") as csvfile, temp_file:
The , temp_file part does strictly nothing. You have to open the file for writing...
and after that you're using temp_file as the output file handle, but it's actually a string, hence the message you're getting.
You want to do this:
with open(filename, "r") as csvfile, open(temp_file,"w",newline='') as outfile:
and then use outfile, not temp_file:
writer = csv.DictWriter(outfile, fieldnames = fieldnames)
Aside: don't use writer.writeheader(): the header is written anyway, you'll get it twice.
Using some help from the internet, I construct a generic function that extracts specific columns from a csv file and reads it to a dictionary, specified by the input keys.
import csv
def dict_filter(it, keys):
for d in it:
yield dict((k, d[k]) for k in keys)
Then I call this method later to write these columns into another CSV file using DictReaderand DictWriter:
fieldnames = ["_STATE", "HEIGHT", "WEIGHT", "_BMI", "AGE", "CTYCODE", "IYEAR"]
source = open("data88.csv", 'r')
reader = csv.DictReader(source)
result = open("aggregate_data.csv", 'w')
writer = csv.DictWriter(result, fieldnames, extrasaction='ignore')
for d in dict_filter(reader, fieldnames):
if d['_STATE'] == "17" :
writer.writerow(str(d))
Here's the error I get in terminal:
AttributeError: 'str' object has no attribute 'get'
In TextWrangler:
Traceback (most recent call last): writer.writerow(str(d))
I've looked all over the internet and am not finding any relief. Why is writerow not working on my instance of DictWriter?
Change the line
writer.writerow(str(d))
to
writer.writerow(d)