Read from csv with, except phrase in " "

时间:2015-09-01 21:40:33

标签: python csv

I have csv file with rows like:

6,0,3,"Moran, Mr. James",male

I read this file with using csv:

import csv
data_iter = csv.reader(dest_f, delimiter=',', quotechar='\n')

As a result, i obtained list, where "Moran and Mr. James" are two different values, but I need to get the complete value in brackets "Moran, Mr. James"

2 个答案:

答案 0 :(得分:1)

Don't specify the quote character (which you have done incorrectly), just let csv.reader use its defaults:

import csv
dest_f = [ '6,0,3,"Moran, Mr. James",male' ]
data_iter = csv.reader(dest_f)
print next(data_iter)

Result:

['6', '0', '3', 'Moran, Mr. James', 'male']

答案 1 :(得分:0)

What about using skipinitialspace ?

>>> import csv
>>> dest_f = '6,0,3,"Moran, Mr. James",male'
>>> for line in csv.reader([dest_f], skipinitialspace=True):
...     print line
... 
['6', '0', '3', 'Moran, Mr. James', 'male']