So I'm trying to use the csv module in python 3.3.2 but I am getting this error.
Traceback (most recent call last):
File "C:\Users\massi_000\Desktop\csv.py", line 1, in <module>
import csv
File "C:\Users\massi_000\Desktop\csv.py", line 4, in <module>
csv.reader(f)
AttributeError: 'module' object has no attribute 'reader'
Obviously I'm going something stupendously wrong but all the code I am using is below and it looks fine. Has something changed in this version that has rendered this code unusable or..?
import csv
f = open("test.csv")
csv.reader(f)
for row in csv_fi:
print(row)
f.close()
You have named your file csv.py and this clashes with the csv module from the Python standard library.
You should rename your own file to something else so that import csv will import the standard library module and not your own. This can be confusing but this is a good rule-of-thumb going forward: avoid giving your own Python files names that are the same as modules in the standard library.
As #Simeon Visser said, you have to rename your file but you have some other issues with your code as well. Try this:
import csv
with open('test.csv', newline='') as f:
reader = csv.reader(f, delimiter=' ')
for row in reader:
print (', '.join(row))
Related
I'm trying to learn python to implement a user agent transformation script in our analytics database.
I imported the user_agents lib to do the conversion and show me the user data. When executing this script in python reading a csv file that I extracted containing the user_agents (the csv has only one column) it returns this error:
TypeError: nailshable type: 'list'
Here is the script I am using:
import csv
from user_agents import parse
with open ('UserAgent.csv', 'r') as csv_file:
csv_reader = csv.reader (csv_file)
for line in csv_reader:
print (parse (line))
The parse method takes a string as an argument. However, in your code, each line is a list and not a string, you can try this:
with open('UserAgent.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for line in csv_reader:
print( parse(' '.join(line)) )
I am trying to convert a json data set file into csv. I am really new to python, and have been looking on the forums and cannot seem to resolve my issues. I have attached the json data url link in below along with my code. Thanks in advance!
https://data.ny.gov/api/views/nqur-w4p7/rows.json?accessType=DOWNLOAD
import json
import csv
inputFile = ("rows.json?accessType=DOWNLOAD", "r")
data = json.load(inputFile)
with open("Data.csv","wb") as csvfile:
csv_writer = csv.DictWriter(csvfile,delimiter=",", fieldnames=["data", "new_york_state_average_gal", "albany_average_gal", "binghamton_average_gal", "bu\
ffalo_average_gal", "nassau_average_gal", "new_york_city_average_gal", "rochester_average_gal", "utica_average_gal"])
csv_writer.writerheader()
csv_writer.writerows(data)
Here is the error I am getting:
File "ChangeDataType.py", line 5, in <module>
data = json.load(inputFile)
File "/usr/lib64/python3.4/json/__init__.py", line 265, in load
return loads(fp.read(),
AttributeError: 'tuple' object has no attribute 'read'
Your error happens because you made a tuple:
inputFile = ("rows.json?accessType=DOWNLOAD", "r")
And you're trying to use json.load in that tuple. Since json.load works only on files, you need to call the open function:
inputFile = open("rows.json?accessType=DOWNLOAD", "r")
The "r" part indicates you're opening the file for reading.
I'd like to open a csv called x with the file name "blah.csv" in a folder.
import csv
def amread(x):
with open('x', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row)
return;
amread() returns name blah.csv is not defined
due to the lack of clarity, here is a full unedited quotation from the interaction pane:
>>> import csv
>>> def amread(x):
... with open('x', newline='') as f:
... reader = csv.reader(f)
... for row in reader:
... print(row)
... return;
...
>>> amread(blah.csv)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'blah' is not defined
>>> amread(blah.csv)
This looks for a name in the python namespace blah.csv. You have no variable or parameter with this name, which causes the error "name blah.csv is not defined".
Assuming what you want is to open a file that is actually named blah.csv, you would pass that name as a string:
>>> amread('blah.csv')
Pandas is pretty good at this, I generally use read_csv.
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html
from dave costa's answer, which is correct:
amread(blah.csv)
This looks for a name in the python namespace blah.csv. You have no variable or parameter with this name, which causes the error "name blah.csv is not defined".
Assuming what you want is to open a file that is actually named blah.csv, you would pass that name as a string:
amread('blah.csv')
I'm getting the following error when attempting to open a new file with today's date.
Traceback (most recent call last):
File "C:\BenPi\stacking\pi3\red_RTS\iotest.py", line 6, in <module>
f = io.open('%s',today, 'w')
TypeError: an integer is required
Here is my code
import datetime
import io
import os
today = datetime.date.today().strftime('%m_%d_%Y')
print (today)
f = io.open('%s',today, 'w')
f.write('first line \n')
f.write('second line \n')
f.close()
It is my understanding that this is an issue that arises when someone inadvertently uses os.open() instead of io.open(), which is why I specified the io option. It should be noted that the same error comes up regardless if I import the os module.
I'm using python 3.2.5
Thoughts?
You're not formatting correctly, you're using , instead of %:
f = io.open('%s'%today, 'w')
Besides, you can just do:
f = io.open(today, 'w')
The line
f = io.open('%s',today, 'w')
should have '%s' first argument the first argument must be the file name.
If you write it like:
f = io.open(today, 'w')
Just works. Also consider using the "with" statment so in case of an exception the stream will be close anyway such as:
with io.open(today, 'w') as f:
f.write("hello world")
I hope I have been helpful.
I am brand new to Python and am having a terrible time trying to read in a .csv file to work with. The code I am using is the following:
>>> dat = open('blue.csv','r')
>>> print dat()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'file' object is not callable
Could anyone help me diagnose this error or lend any suggestions on how to read the file in? Sorry if there is an answer to this question already, but I couldn't seem to find it.
You need to use read in order to read a file
dat = open('blue.csv','r')
print dat.read()
Alternatively, you can use with for self-closing
with open('blue.csv','r') as o:
data = o.read()
You can read the file:
dat = open('blue.csv', 'r').read()
Or you can open the file as a csv and read it row by row:
import csv
infile = open('blue.csv', 'r')
csvfile = csv.reader(infile)
for row in csvfile:
print row
column1 = row[0]
print column1
Check out the csv docs for more options for working with csv files.