Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read CSV file with comma within fields in Python

Tags:

python

csv

I need to read a CSV file which has fields that have a comma, so I have double quoted the fields which contains commas, such as:

1, "text1,text2", "text3, text4", a, b, c 

But when I try to read the file in Python I get the fields separated by the commas, as following:

row[0] = 1 row[1] = text1 row[2] = text2 row[3] = text3 row[4] = text4 row[5] = a row[6] = b row[7] = c 

I am reading the CSV file with the following code:

info = csv.reader(open('./info.csv'))   for row in info :     print row[0] + " * " + row[1] ... 

Is it possible to read double quoted fields which contains a comma?

like image 945
David Avatar asked Nov 29 '11 13:11

David


People also ask

How do you read a CSV file with commas within a field in Python?

info = csv. reader(open('./info. csv')) for row in info : print row[0] + " * " + row[1] ... Is it possible to read double quoted fields which contains a comma?

How do you handle commas in data in a CSV file in Python?

A CSV file is a simple text file where each line contains a list of values (or fields) delimited by commas. Although the term "Comma" appears in the format name itself, but you will encounter CSV files where data is delimited using tab ( \t ) or pipe ( | ) or any other character that can be used as a delimiter.

How read comma separated CSV file in pandas?

The pandas DataFrame class supports serializing and de-serializing of CSV in an extenstive way through the read_csv() method. The read_csv() method of pandas DataFrame class reads a CSV file and loads each record as a row in the DataFrame.


1 Answers

The Python csv module actually does support quoted fields, even by default. Your problem here is that Python by default does not skip the space, so you need to use skipinitialspace=True.

>>> s = StringIO.StringIO('1, "text1,text2", "text3, text4", a, b, c') >>> list(csv.reader(s, skipinitialspace=True)) [['1', 'text1,text2', 'text3, text4', 'a', 'b', 'c']] 
like image 104
Sven Marnach Avatar answered Sep 16 '22 21:09

Sven Marnach