Jason Swett
Jason Swett

Reputation: 45074

Python CSV reader isn't reading CSV data the way I expect

I'm trying to read some CSV data into an array. I can probably explain what I'm trying to do better in Python than in English:

>>> line = ImportFile.objects.all().reverse()[0].file.split("\n")[0]
>>> line
'"007147","John Smith","100 Farley Ln","","Berlin NH 03570","Berlin","NH",2450000,"John",24643203,3454,"E","",2345071,1201,"N",15465,"I",.00,20102456,945610,20247320,1245712,"0T",.00100000,"",.00,.00,780,"D","000",.00,0\r'
>>> s = cStringIO.StringIO()
>>> s
<cStringIO.StringO object at 0x9ab1960>
>>> s.write(line)
>>> r = csv.reader(s)
>>> r
<_csv.reader object at 0x9aa217c>
>>> [line for line in r]
[]

As you can see, the CSV data starts in memory, not in a file. I would expect my reader to have some of that data but it doesn't. What am I doing wrong?

Upvotes: 4

Views: 1725

Answers (2)

Sven Marnach
Sven Marnach

Reputation: 601371

You are using StringIO in the wrong way. Try

s = cStringIO.StringIO(line)
r = csv.reader(s)
next(r)
# "['007147', 'John Smith', '100 Farley Ln', '', 'Berlin NH 03570', 'Berlin', 'NH', '2450000', 'John', '24643203', '3454', 'E', '', '2345071', '1201', 'N', '15465', 'I', '.00', '20102456', '945610', '20247320', '1245712', '0T', '.00100000', '', '.00', '.00', '780', 'D', '000', '.00', '0']"

and the result should be what you expect.

Edit: To explain in more detail: After writing to the StringIO instance, the file pointer will point past the end of the contents. This is where you would expect new contents to be written by subsequent write() calls. But this also means that read() calls will not return anything. You would need to call s.reset() or s.seek(0) to reset the position to the beginning, or initialise the StringIO with the desired contents.

Upvotes: 6

9000
9000

Reputation: 40884

Add s.seek(0) after s.write(line). Current pointer in the file-like object s is just past the written line.

Upvotes: 0

Related Questions