Trying to remove extra characters created when writing CSV file
I'm trying to take a comma separated text block from the clipboard and convert it to a CSV file. A sample text that I'm using in test looks like this:
apple, orange, pear bread , donut, bagel car, toy, truck
My current script is supposed to read the text from the clipboard and create a CSV file:
import csv import clipboard data = clipboard.get() f = open('cv_data.csv', 'wb') w = csv.writer(f, delimiter = ',') w.writerow(data.split(',')) f.close()
I get the file to write, but the output from the script ends up being this:
apple, orange," pear bread", donut," bagel car", toy, truck
Any suggestions on what I can do differently to not get the extra quotes in the output file?
Given your sample data, you probably want to call writerow() three times instead of just once. In your app above, you are writing all data to a single row and writerow() is putting quotes around those elements that contain a carriage return.
import clipboard, csv data = clipboard.get() #.replace(' ', '') with open('csv_data.csv', 'w') as out_file: w = csv.writer(out_file) for line in data.splitlines(): w.writerow(line.split(','))
Perfect! Thanks for the help.