Python: Remove Extra Quotes when Exporting to CSV -


i trying export list csv keeps giving me quotes. looks when try export row:

"strawberry, red"

what want like:

strawberry, red

this code:

import csv  def writetocsv(filename,writerow):     f = open(filename, 'ab')     outputfile = csv.writer(f)     outputfile.writerow(writerow)  def csv_to_list_noheaders(user_file):     listoffruits = []     open(user_file) output:         reader = csv.reader(output)         next(reader, none)         each_line in output:             listoffruits.append(each_line)     return listoffruits  filename1 = 'yesterdayfruits.csv' filename2 = 'todaysfruits.csv' fileoutput = 'fruitsmissing.csv'  todaysfruits = csv_to_list_noheaders('todaysfruits.csv') yesterdayfruits1 = csv_to_list_noheaders('yesterdayfruits.csv') yesterdayfruits2 = [] missingfruits = []  yesterday in range(len(yesterdayfruits1) - 1, -1, -1):     today in range(0, len(todaysfruits), 1):         if (yesterdayfruits1[yesterday].split(',')[0].strip() ==  todaysfruits[today].split(',')[0].strip()):             yesterdayfruits2.append(yesterdayfruits1[yesterday])  x in yesterdayfruits1:     if x in yesterdayfruits2:         pass     else:         missingfruits.append(x)  y in missingfruits:     writetocsv('fruitsmissing.csv', [y.strip()]) 

i have tried doing:

 outputfile = csv.writer(f,quoting=csv.quote_none) 

but is:

_csv.error: need escape, no escapechar set

yesterdayfruits.csv file contains:

fruit name, fruit color apple, red orange, orange banana, yellow watermelon, green strawberry, red 

todaysfruits.csv file contains:

fruit name, fruit color apple, red orange, orange banana, yellow watermelon, green 

the csv function writerow expects list of strings. if want output row have 2 values, need pass 2-list. code passing 1-list containing single string comma in it.

that 1-list gets interpreted row single value contains embedded comma. if value has embedded comma, writerow has put quotes around it, because otherwise program reads file interpret 2 values, , because writerow getting 1-list, knows there 1 value in row.

that why getting quotes. , when this:

outputfile = csv.writer(f,quoting=csv.quote_none) 

csv complains has indicate somehow comma part of data have (a) forced quoting off , (b) supplied no alternative way escape comma.

so, need fix line:

writetocsv('fruitsmissing.csv', [y.strip()]) 

y string contains "strawberry, red". instead arrange list looks this: ["strawberry", "red"] , call function this:

writetocsv('fruitsmissing.csv', y) 

essentially, code working hard. setting line line of text commas in already. csv's job.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -