Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does python 3.1.3 support unicode in csv module?

Tags:

python

csv

I have been using python 2.6. While I was writing a python program to process the query result ( in csv format ) from sql server. I found it does not support unicode.

When I run the program with csv file, a error poped up saying:

    for row in csvReader:
Error: line contains NULL byte

After I save the csv file in ANSI/ASCII format with Ultraedit, the program is running okay.

I tried to include the encoding option, but it failed:

csvReader = csv.reader(open(fname, mode='rb', encoding='unicode'), delimiter=',')
TypeError: 'encoding' is an invalid keyword argument for this function

csvReader = csv.reader(open(fname, mode='rb', encoding='utf-8'), delimiter=',')
TypeError: 'encoding' is an invalid keyword argument for this function

I wonder if python 3 support this unicode reading. It can save me a lot of work.

like image 809
lamwaiman1988 Avatar asked Feb 29 '12 06:02

lamwaiman1988


People also ask

What modules read CSV files in Python?

Reading a CSV File There are various ways to read a CSV file that uses either the CSV module or the pandas library. csv Module: The CSV module is one of the modules in Python which provides classes for reading and writing tabular information in CSV file format.

How do you write values in a CSV file in Python?

DictWriter() class can be used to write to a CSV file from a Python dictionary. Here, file - CSV file where we want to write to. fieldnames - a list object which should contain the column headers specifying the order in which data should be written in the CSV file.

What modules can be imported to CSV?

Python CSV Module Python provides a CSV module to handle CSV files. To read/write data, you need to loop through rows of the CSV. You need to use the split method to get data from specified columns.


2 Answers

Python 3 definitely supports unicode. My guess is that you specified the wrong (or no?) encoding when you opened the CSV file for reading. See: http://docs.python.org/release/3.1.3/library/functions.html#open

And try something like:

reader = csv.reader(open("foo.csv", encoding="utf-8"))

Edit: If you are using Python 2.6, you can achieve the same result with:

import codecs
reader = csv.reader(codecs.open("foo.csv", encoding="utf-8"))

HOWEVER if you're getting null bytes, your file may be encoded using "utf-16", so try that if the file can't be decoded using utf-8.

like image 192
David Wolever Avatar answered Nov 02 '22 16:11

David Wolever


A similar question is already answered Python CSV error: line contains NULL byte

Also, Try to open it in 'rb' mode instead of 'rU'

like image 34
jerrymouse Avatar answered Nov 02 '22 16:11

jerrymouse