Common problem in CSV file writing
First let me tell you how I write CSV files :)
I don't use Pythons CSV module for writing. I just name the file xyz.csv (fp = open('xzy.csv', 'w')) and write using something like: fp.write(a+','+b+','+c)
But sometimes it can create a problem if any string contains a comma (,) within itself. For example if the string a is 'hello, how are you?', then it will be printed in two columns... so before writing to file I use:
a = a.replace(',', ' ')
Is there any other way to avoid such situation? If you know, please share.
I don't use Pythons CSV module for writing. I just name the file xyz.csv (fp = open('xzy.csv', 'w')) and write using something like: fp.write(a+','+b+','+c)
But sometimes it can create a problem if any string contains a comma (,) within itself. For example if the string a is 'hello, how are you?', then it will be printed in two columns... so before writing to file I use:
a = a.replace(',', ' ')
Is there any other way to avoid such situation? If you know, please share.
Comments
- Paddy.
By the way, you could use e.g. '&c;' to replace commas, and '&a;' and '&s;' to replace '&' and ';'. Not very nice though...
BTW, I found another way to get rid of the problem. Check the code:
name = '"'+item[0]+'"'
address = '"'+item[1]+'"'
phone = '"'+item[2]+'"'
fout.write(name+","+address+","+phone+"\n")
@aatiis, thanks for your review.