Saturday 15 August 2015

python - CSV with spaces around the comma -


Assume that I have a CSV file with a space near the comma:

  '1' If I use the Python CSV package, then  4, '2', '3', '4' 5 ',' 6 ',' 7 ',' 8 '  

and 8 values ​​are treated differently:

  & gt; & Gt; & Gt; ('/ Tmp / nums.csv', 'ru') as the funnel ... ... in the csv.reader for the row (fin, quotation = "'"): ... print line ... [ '1', '2', '3', '' 4 ''] ['5', '6', '7', '' 8 '']   

CSV module Fix it using? I know that I can read and parse the file myself, but I'm interested in that if there is a bid setting in the CSV package to fix it.

Set to leave any white spot after a delimiter:

White space has been ignored immediately after True , Delimiter . The default is incorrect .

Demo:

  & gt; & Gt; & Gt; Import CSV & gt; & Gt; & Gt; Demo = '' '... 1', '2', '3', '4' ... '5', '6', '7', '8' ... '' '& Gt; & Gt; & Gt; For queue in csv.reader (demo splitlines (True), skipinitialspace = true, quotechar = "'"): ... print line ... [' 1 ',' 2 ',' 3 ',' 4 ' ] ['5', '6', '7', '8']    

No comments:

Post a Comment