Inline csv to list loader (not recommended) - Python

This is a nice trick that once again shows the power of Python. But I do not recommend to use this solution in a regular programming or at least check for any errors while opening a file.

Example 1 -
csv file (test1.csv):

line 1 field 1,line 1 field 2,line 1 field 3,line 1 field 4
line 2 field 1,line 2 field 2,line 2 field 3,line 2 field 4
line 3 field 1,line 3 field 2,line 3 field 3,line 3 field 4

loading as a list

import re

csv = [ re.split(",",l) for l in re.split("\n",open("test1.csv").read()) ]
print csv

result

[
['line 1 field 1', 'line 1 field 2', 'line 1 field 3', 'line 1 field 4'],
['line 2 field 1', 'line 2 field 2', 'line 2 field 3', 'line 2 field 4'],
['line 3 field 1', 'line 3 field 2', 'line 3 field 3', 'line 3 field 4']
]



Example 2 -
csv file (test2.csv):

"line 1,field 1","line 1,field 2","line 1,field 3","line 1,field 4"
"line 2,field 1","line 2,field 2","line 2,field 3","line 2,field 4"
"line 3,field 1","line 3,field 2","line 3,field 3","line 3,field 4"

loading as a list:

import re

csv = [ re.findall('"[^"]+"',l) for l in re.split("\n",open("test2.csv").read()) ]
print csv

result

[
['"line 1,field 1"', '"line 1,field 2"', '"line 1,field 3"', '"line 1,field 4"'],
['"line 2,field 1"', '"line 2,field 2"', '"line 2,field 3"', '"line 2,field 4"'],
['"line 3,field 1"', '"line 3,field 2"', '"line 3,field 3"', '"line 3,field 4"']
]


Example 3
and instruction that will handle all two files:

import re

csv = [ re.findall('"[^"]+"',l) if re.match("\"",l) else re.split(",",l) for l in re.split("\n",open("test2.csv").read()) ]
print csv

result are the same like in example 1 or 2.

Any questions and suggestions are welcome.

0 comments:

Post a Comment