Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a list in python when the elements contain commas

Tags:

python

list

csv

How do I create a list of strings in python where the strings can contain commas such that the interpreter doesn't break on commas prematurely.

I am reading in a template csv file, created by MSExcel. The contents are 2 columns: col1 = color, col2 = a sentence that can contain a comma. The content is as follows:

Light Green,"Matches type, instance, etc."
Red,Happens in BD10 but not BD20
Blue,"Instance and time matches, type doesn't match"
Yellow,Caught on replay

When I read in and output what was read, it breaks the input line on all commas.

#my code snippet, please excuse any typos during sanitization
with open('outfile.csv','r') as infile:
   blah = infile.readlines()
   for i in blah:
      line = i.strip().split(",")
      print line

My output:
['Light Green', '"Matches type', ' instance', ' etc."']
['Red', 'Happens in BD10 but not BD20']
['Blue', '"Instance and time matches', ' type doesn\'t match"']
['Yellow', 'Caught on replay']

How do I tell python to ignore commas when it's within "" and break at commas all other times?

like image 543
Classified Avatar asked Apr 16 '26 15:04

Classified


2 Answers

python provides tools to parse csv files ... you should use them

print list(csv.reader(open("outfile.csv","r")))

maybe?

like image 116
Joran Beasley Avatar answered Apr 18 '26 05:04

Joran Beasley


The cvs module makes this easy as pie.

Given:

$ cat /tmp/so.csv
Light Green,"Matches type, instance, etc."
Red,Happens in BD10 but not BD20
Blue,"Instance and time matches, type doesn't match"
Yellow,Caught on replay

Try:

import csv
with open('/tmp/so.csv') as f:
    for line in csv.reader(f):
        print line    

Prints:

['Light Green', 'Matches type, instance, etc.']
['Red', 'Happens in BD10 but not BD20']
['Blue', "Instance and time matches, type doesn't match"]
['Yellow', 'Caught on replay']
like image 27
dawg Avatar answered Apr 18 '26 05:04

dawg