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.

1
  • 2
    Please add a few lines of your input file to your question so we can try to recreate the problem. Commented Mar 29, 2015 at 18:46

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.