Rob B.
Rob B.

Reputation: 128

Python CSV Module

I'm using the following code to learn how to read, write and analyze with a CSV file from this website http://www.whit.com.au/blog/2011/11/reading-and-writing-csv-files/. However, I get these errors,

Traceback (most recent call last): Files "csv_example.py", 
line 1, in <module? import csv
File "C:\python27\csv.py", line 11, in <module>
mywriter = csv.writer(csv_out)
AttributeError: 'module' object has no attribute 'writer'

Do you have any idea why I am getting the errors above? I'm using Notepad++ and PowerShell. I actually looked around and tried to find a CSV module for python to download but I didn't find any. I'm at a bit of a lost.

import csv

bus_numbers = ['101', '102', '36', '40']
bus_names = ['NUC_A', 'NUC_B', 'CATDOG', 'HYDRO_A']
voltage = [.99, 1.02, 1.01, 1.00]

# open a file for writing.
csv_out = open('mycsv.csv', 'wb')

# create the csv writer object.
mywriter = csv.writer(csv_out)

# all rows at once.
rows = zip(bus_numbers, bus_names, voltage)
mywriter.writerows(rows)

# always make sure that you close the file.
# otherwise you might find that it is empty.
csv_out.close()

On a sidenote, I am interesting in good tutorials on how to read, write and use CSV files in python.

Upvotes: 2

Views: 8275

Answers (1)

Lie Ryan
Lie Ryan

Reputation: 64933

This is a common error. Rename your script from csv.py to something else; it will try to import itself when you do import csv.

Upvotes: 11

Related Questions