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?
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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With