Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python csv module error

Tags:

python

csv

When I use Pythons csv module, it shows me

"delimiter" must be an 1-character string"

My code is like this

 sep = ","
 srcdata = cStringIO.StringIO(wdata[1])
 data = csv.reader(srcdata, delimiter=sep)

wdata[1] is a string source.

How do I fix this problem?

like image 632
alwx Avatar asked Apr 11 '11 17:04

alwx


People also ask

Why is CSV not importing?

Common issues with importing a CSV (and the work-arounds to fix them) The most common CSV import errors include: The file size is too large - The CSV import tool of the program you're using might have a file size requirement. To reduce the file size, you can delete unnecessary data values, columns, and rows.

What is Python CSV module?

The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,” or “read data from this file which was generated by Excel,” without knowing the precise details of the CSV format used by Excel.

Why is CSV not defined in Python?

The Python "NameError: name 'csv' is not defined" occurs when we use the csv module without importing it first. To solve the error, import the csv module before using it - import csv .


1 Answers

You most likely have from __future__ import unicode_literals at the top of your module or you are using python 3.x+ You need to do something like this:

sep=b","  # notice the b before the "
srcdata=cStringIO.StringIO(wdata[1])
data = csv.reader(srcdata,delimiter=sep)

This tells Python that you want to represent "," as a byte string instead of a unicode literal.

like image 150
Mahmoud Abdelkader Avatar answered Oct 19 '22 00:10

Mahmoud Abdelkader