CSV delimiter doesn't work properly [Python]

import csv

base='[email protected],username1\
[email protected],username2\
[email protected],username3\
[email protected],username4\
[email protected],username5'

parsed=csv.reader(base, delimiter=',')
for p in parsed:
    print p

Returns:

['e']
['e']
['s']
['t']
['1']
['@']
['m']
['a']
['i']
['l']
['.']
['r']
['u']
['', ''] 

etc...

How I can get data separated by comma ? ('[email protected]', 'username1'), ('[email protected]', 'username2'), ...

Upvotes: 0

Views: 1413

Answers (2)

Łukasz Rogalski
Łukasz Rogalski

Reputation: 23243

Quoting official docs on csv module (emphasis mine):

csv.reader(csvfile, dialect='excel', **fmtparams)

Return a reader object which will iterate over lines in the given csvfile. csvfile can be any object which supports the iterator protocol and returns a string each time its __next__() method is called — file objects and list objects are both suitable.

Strings supports iterator, but it yields characters from string one by one, not lines from multi-line string.

>>> s = "abcdef"
>>> i = iter(s)
>>> next(i)
'a'
>>> next(i)
'b'
>>> next(i)
'c'

So the task is to create iterator, which would yield lines and not characters on each iterations. Unfortunately, your string literal is not a multiline string.

base='[email protected],username1\
[email protected],username2\
[email protected],username3\
[email protected],username4\
[email protected],username5'

is equivalent to:

base = '[email protected],[email protected],[email protected],[email protected],[email protected],username5

Esentially you do not have information required to parse that string correctly. Try using multiline string literal instead:

base='''[email protected],username1
[email protected],username2
[email protected],username3
[email protected],username4
[email protected],username5'''

After this change you may split your string by newlines characters and everything should work fine:

parsed=csv.reader(base.splitlines(), delimiter=',')
for p in parsed:
    print(p)

Upvotes: 1

sberry
sberry

Reputation: 132138

I think csv only works with file like objects. You can use StringIO in this case.

import csv
import StringIO

base='''[email protected],username
[email protected],username2
[email protected],username3
[email protected],username4
[email protected],username5'''

parsed=csv.reader(StringIO.StringIO(base), delimiter=',')
for p in parsed:
    print p

OUTPUT

['[email protected]', 'username']
['[email protected]', 'username2']
['[email protected]', 'username3']
['[email protected]', 'username4']
['[email protected]', 'username5']

Also, your example string does not have newlines, so you would get

['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', 'username5']

You can use the ''' like I did, or change your base like

base='[email protected],username\n\
[email protected],username2\n\
[email protected],username3\n\
[email protected],username4\n\
[email protected],username5'

EDIT
According to the docs, the argument can be either a file-like objet OR a list. So this works too

parsed=csv.reader(base.splitlines(), delimiter=',')

Upvotes: 1

Related Questions