0

I am trying to read a pipe separated csv file using python read_csv() but it is incorrectly detecting the double quotes in the first field. I know it has something to do with csv reader flags but unable to figure it out.

CODE

with open('Find_File.dat', 'r') as csvfile:
myfile=csv.reader(csvfile,escapechar=" ' ",quoting=csv.QUOTE_NONE,quotechar="'",doublequote=True)
for row in myfile:
     data.append(row)  
print(data[0]) 

EXPECTED OUTPUT

["TCC Record Identifier"|"UserName"|"Candidate ID"|"Experience"|"Job Sequence"|"Current 10"|"Ending Rate of Pay 10"]

ACTUAL OUTPUT

['TCC Record Identifier|"UserName"|"Candidate ID"|"Experience"|"Job Sequence"|"Current 10"|"Ending Rate of Pay 10" ']

RamblinRose
  • 4,883
  • 2
  • 21
  • 33
  • 1
    Could you please post a raw one or two line sample from the csv file you're parsing? It's hard to know where parsing may be failing without seeing the source data. If possible, include the problem in a way that's fully reproducible for others to maximize your chances of receiving help. – Julian Mar 18 '20 at 19:00
  • **Please provide a [mcve].** Also, the expected output is a bit odd, please clarify things. – AMC Mar 18 '20 at 19:16

1 Answers1

1

Your expected output is a bit odd. Wouldn't you want to parse the line into a list of columns? Also, if the delimiter is pipe, that wasn't specified.

Given that the input file looks like:

"TCC Record Identifier"|"UserName"|"Candidate ID"|"Experience"|"Job Sequence"|"Current 10"|"Ending Rate of Pay 10"

or even:

TCC Record Identifier|UserName|Candidate ID|Experience|Job Sequence|Current 10|Ending Rate of Pay 10

The default csv.reader will parse either of the above inputs by simply overriding the delimiter:

import csv

with open('input.csv',newline='') as f:
    r = csv.reader(f,delimiter='|')
    for row in r:
        print(row)

Output:

['TCC Record Identifier', 'UserName', 'Candidate ID', 'Experience', 'Job Sequence', 'Current 10', 'Ending Rate of Pay 10']
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251