For the first time I come across a CSV format. For experiments, I created an example.csv file with the following contents:

 first name, last name, module1, module2, module3, description student, best, 100, 100, 100, Excellent score student, good, 90, "90,2", 100, "Good score but could do better" 

The problem is that, as far as I understand, the values ​​separated by quotes should go together as one value. But when I run the following script:

 import csv with open("D:\\pytest\example.csv") as f: reader = csv.reader(f) for row in reader: print(row) 

I see the following result:

 ['first name', ' last name', ' module1', ' module2', ' module3', ' description'] ['student', ' best', ' 100', ' 100', ' 100', ' Excellent score'] ['student', ' good', ' 90', ' "90', '2"', ' 100', ' "Good score'] ['but could do better"'] 

Why are 90 and 2 in the third line going separately and at the end of the line is not written together?

    1 answer 1

    I think your csv file is not exactly a csv file, because there are no spaces after the commas

     >>> csv_text = """first name,last name,module1,module2,module3,description ... student,best,100,100,100,Excellent score ... student,good,90,"90,2",100,"Good score ... but could do better" ... """ >>> f = StringIO.StringIO(csv_text) >>> list(csv.reader(f)) [['first name', 'last name', 'module1', 'module2', 'module3', 'description'], ['student', 'best', '100', '100', '100', 'Excellent score'], ['student', 'good', '90', '90,2', '100', 'Good score\nbut could do better']] 

    However, this problem can also be solved with the skipinitialspace argument:

     >>> csv_text2 = """first name, last name, module1, module2, module3, description ... student, best, 100, 100, 100, Excellent score ... student, good, 90, "90,2", 100, "Good score ... but could do better" """ >>> >>> f = StringIO.StringIO(csv_text2) >>> list(csv.reader(f, skipinitialspace=True)) [['first name', 'last name', 'module1', 'module2', 'module3', 'description'], ['student', 'best', '100', '100', '100', 'Excellent score'], ['student', 'good', '90', '90,2', '100', 'Good score\nbut could do better ']]