I am trying to create a python script that adds quotations around part of a string, after 3 commas
So if the input data looks like this:
1234,1,1/1/2010,This is a test. One, two, three.
I want python to convert the string to:
1234,1,1/1/2010,"This is a test. One, two, three."
The quotes will always need to be added after 3 commas
I am using Python 3.1.2 and have the following so far:
i_file=open("input.csv","r")
o_file=open("output.csv","w")
for line in i_file:
tokens=line.split(",")
count=0
new_line=""
for element in tokens:
if count = "3":
new_line = new_line + '"' + element + '"'
break
else:
new_line = new_line + element + ","
count=count+1
o_file.write(new_line + "\n")
print(line, " -> ", new_line)
i_file.close()
o_file.close()
The script closes immediately when I try to run it and produces no output
Can you see what’s wrong?
Thanks
Having addressed the two issues mentioned in my comment above I’ve just tested that the code below (edit: ALMOST works; see very short code sample below for a fully tested and working version) for your test input.
Side note:
A relatively new feature in Python is the
withstatement. Below is an example of how you might take advantage of that more-robust method of coding (note that you don’t need to add theclose()calls at the end of processing):