Search code examples
pythoncsvreader

CSV Reader Removing Double Quotes from First Field


I have a file which contains a tab delimited header and line like so:

ID  Field1
test1   "A","B"

Here's my parsing script.

with open(dataFile) as tsv:
    for line in csv.reader(tsv, delimiter='\t'):
        print(line)

And the output:

['ID', 'Field1']
['test1', 'A,"B"']

I can't figure out why it's stripping the double quotes on the first quoted item of the second field. I've tried different dialects and settings for csv reader with no success.


Solution

  • You just need to tell the csv.reader to ignore quoting, via the csv.QUOTE_NONE option:

    with open(dataFile) as tsv:
        for line in csv.reader(tsv, delimiter='\t', quoting=csv.QUOTE_NONE):
            print(line)
    

    Output:

    ['ID', 'Field1']
    ['test1', '"A","B"']