I have a .csv file like the sample below:
27/04/22;8888888;LBB blablalbla;5 000;5 000;0;0;53707;1;WWW;
27/04/22;8888888;"LBB blablalbla;15 000;15 000;0;0;53707;1;WWW;
27/04/22;8888888;LBB blablalbla;10 000;10 000;0;0;53707;1;WWW;
I am not able to read it correctly with csv python module because on the second line there is a double quote but no end of quote. The result is that the rest of the csv file is stored in the same dict value.
I am using csv.DictReader function but I do not find any option to not take this double quote into account.
Do I need to rewrite the .csv file before working with this function?
CodePudding user response:
Do I need to rewrite the .csv file before working with this function?
No, you can use csv.QUOTE_NONE
as follows, let file.csv
content be
27/04/22;8888888;LBB blablalbla;5 000;5 000;0;0;53707;1;WWW;
27/04/22;8888888;"LBB blablalbla;15 000;15 000;0;0;53707;1;WWW;
27/04/22;8888888;LBB blablalbla;10 000;10 000;0;0;53707;1;WWW;
then
import csv
with open('file.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile,fieldnames=["A","B","C","D","E","F","G","H","I","J","K"],delimiter=";",quoting=csv.QUOTE_NONE)
for row in reader:
print(row)
gives output
OrderedDict([('A', '27/04/22'), ('B', '8888888'), ('C', 'LBB blablalbla'), ('D', '5 000'), ('E', '5 000'), ('F', '0'), ('G', '0'), ('H', '53707'), ('I', '1'), ('J', 'WWW'), ('K', '')])
OrderedDict([('A', '27/04/22'), ('B', '8888888'), ('C', '"LBB blablalbla'), ('D', '15 000'), ('E', '15 000'), ('F', '0'), ('G', '0'), ('H', '53707'), ('I', '1'), ('J', 'WWW'), ('K', '')])
OrderedDict([('A', '27/04/22'), ('B', '8888888'), ('C', 'LBB blablalbla'), ('D', '10 000'), ('E', '10 000'), ('F', '0'), ('G', '0'), ('H', '53707'), ('I', '1'), ('J', 'WWW'), ('K', '')])