2

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.

1
  • The official Python documentation has a well elaborated section on csv module Commented Oct 15, 2013 at 2:20

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.