This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: csv library does not correctly interpret some files
Type: Stage:
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: voidloop
Priority: normal Keywords:

Created on 2021-07-08 13:13 by voidloop, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg397139 - (view) Author: Marco E. (voidloop) Date: 2021-07-08 13:13
The CSV library does not correctly interpret files in the following format (test.csv):

"A"     ,"B"      ,"C"
"aaaaaa","bbbbbbb","cccc"
"aaaaa" ,"bbbbbb" ,"ccc"
"aaaa"  ,"bbbbb"  ,"cc"


This program:

import csv
from pathlib import Path


def main():
    with Path('test.csv').open('rt') as csv_file:
        csv.register_dialect('my_dialect', quotechar='"', delimiter=',',
                             quoting=csv.QUOTE_ALL, skipinitialspace=True)
        reader = csv.DictReader(csv_file, dialect='my_dialect')
        for row in reader:
            print(row)


if __name__ == '__main__':
    main()


produces the following output:

{'A     ': 'aaaaaa', 'B      ': 'bbbbbbb', 'C': 'cccc'}
{'A     ': 'aaaaa ', 'B      ': 'bbbbbb ', 'C': 'ccc'}
{'A     ': 'aaaa  ', 'B      ': 'bbbbb  ', 'C': 'cc'}


this instead is the expected result:

{'A': 'aaaaaa', 'B': 'bbbbbbb', 'C': 'cccc'}
{'A': 'aaaaa', 'B': 'bbbbbb', 'C': 'ccc'}
{'A': 'aaaa', 'B': 'bbbbb', 'C': 'cc'}


why?

Thank you,
Marco
History
Date User Action Args
2022-04-11 14:59:47adminsetgithub: 88751
2021-07-08 13:13:28voidloopcreate