12

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?

2
  • 6
    Can you post the actual error message that you receive? Commented Apr 11, 2011 at 18:00
  • 1
    You should accept Mahmoud's answer -- click on the checkmark near it. Commented Nov 21, 2011 at 21:16

1 Answer 1

31

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.

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

1 Comment

I now get TypeError: delimiter must be set under Python 3.3 and "delimiter" must be a 1-character string under Python 3.2.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.