简体   繁体   中英

Escape single quote in CSV file

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?

Do I need to rewrite the.csv file before working with this function?

No, you can usecsv.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', '')])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM