2

When I run this input (saved as variable 'line'):

xsc_i,202,"House of Night",21,"/21_202"

through a csv reader:

for row in csv.reader(line):
    print row

it splits the strings, not just the fields

['x']
['s']
['c']
['_']
['i']
['', '']
['2']
['0']
['2']
['', '']

etc.

It exhibits this behavior even if I explicitly set the delimiter:

csv.reader(line, delimiter=",")

It's treating even strings as arrays, but I can't figure out why, and I can't just split on commas because many commas are inside "" strings in the input.

Python 2.7, if it matters.

3 Answers 3

6

The first argument to csv.reader() is expected to be an iterable object containing csv rows. In your case the input is a string (which is also iterable) containing a single row. You need to enclose the line into a list:

for row in csv.reader([line]):
    print row

Demo:

>>> import csv
>>> line = 'xsc_i,202,"House of Night",21,"/21_202"'
>>> for row in csv.reader([line]):
...     print row
... 
['xsc_i', '202', 'House of Night', '21', '/21_202']
Sign up to request clarification or add additional context in comments.

2 Comments

Great, up-voted- if we change input as >>> line = ''''xsc_i',202,"House of Night",21,"/21_202"''' then it produce output as ["'xsc_i'", '202', 'House of Night', '21', '/21_202'] , so question is why ' is coming and why " is not coming in a output ?
@Clay Shrinky: why do you care about ' and "? You shouldn't.
1

Just in case you want to see re in action.

import re
line='xsc_i,202,"House of Night",21,"/21_202"'
print map(lambda x:x.strip('"'),re.split(r',(?=(?:[^"]*"[^"]*")*[^"]*$)',line))

Output:['xsc_i', '202', 'House of Night', '21', '/21_202']

Comments

1

This is because csv.reader expects

any object which supports the iterator protocol and returns a string each time its next() method is called

You have passed a string to the reader.

If you say:

line = ['xsc_i,202,"House of Night",21,"/21_202"',]

Your code should work as expected. Please see docs

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.