0

I have two programs that I converted to using the csv module in Python 2.7. The first I just used csv.reader:

f=open(sys.argv[1],'r')
line=csv.reader(f)
for values in line:
  …do some stuff…

In this case commas within quoted string are removed. Another program was too complex just to read so I copied the file changing the delimiter to semi-colon:

fin=open(sys.argv[1],'rb')   
lines=csv.reader(fin)
with open('temp.csv','wb') as fout:
  w=csv.writer(fout,delimiter=';')
  w.writerows(lines)

In this case the commas inside of quoted string are preserved. I find no posts on bugs.python.org on this, so I conclude it must be me. So, anyone else experience this behavior. In the second case all elements are quoted. In the first case quotes are used only where required.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
dtd
  • 51
  • 1
  • 5

1 Answers1

0

It's hard to guess without seeing a sample of your input file and the resulting output, but maybe this is related: Read CSV file with comma within fields in Python

Try csv.reader(f, skipinitialspace=True).

Community
  • 1
  • 1
michailgames
  • 303
  • 2
  • 7