Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Remove Extra Quotes when Exporting to CSV

I am trying to get export a list to csv but it keeps giving me extra quotes. This is what it looks like when i try to export the row:

"strawberry, red"

What i want it to look like:

strawberry, red

This is my 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 = []
    with open(user_File) as output:
        reader = csv.reader(output)
        next(reader, None)
        for 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 = []

for yesterday in range(len(yesterdayfruits1) - 1, -1, -1):
    for today in range(0, len(todaysfruits), 1):
        if (yesterdayfruits1[yesterday].split(',')[0].strip() == 
todaysfruits[today].split(',')[0].strip()):
            yesterdayfruits2.append(yesterdayfruits1[yesterday])

for x in yesterdayfruits1:
    if x in yesterdayfruits2:
        pass
    else:
        missingFruits.append(x)

for y in missingFruits:
    writeToCSV('fruitsMissing.csv', [y.strip()])

I have tried doing:

 outputFile = csv.writer(f,quoting=csv.QUOTE_NONE)

But all I get is:

_csv.Error: need to escape, but 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
like image 503
J. D. Avatar asked Mar 22 '26 02:03

J. D.


1 Answers

The csv function writerow expects to get a list of strings. If you want the output row to have two values, you need to pass it a 2-list. But your code is passing it a 1-list containing a single string with a comma in it.

That 1-list gets interpreted as a row with single value that contains an embedded comma. If a value has an embedded comma, writerow has to put quotes around it, because otherwise the program that reads the file would interpret it as two values, and because writerow is getting a 1-list, it knows that there is only one value in the row.

That is why you are getting the quotes. And when you do this:

outputFile = csv.writer(f,quoting=csv.QUOTE_NONE)

csv complains that it has to indicate somehow that the comma is part of the data but you have (a) forced quoting off and (b) supplied no alternative way for it to escape the comma.

So, you need fix this line:

writeToCSV('fruitsMissing.csv', [y.strip()])

y is currently a string that contains "strawberry, red". Instead arrange for it to be a list that looks like this: ["strawberry", "red"] and call your function like this:

writeToCSV('fruitsMissing.csv', y)

Essentially, your code is working too hard. It is setting up a line a line of text with the commas in it already. That is csv's job.

like image 164
BoarGules Avatar answered Mar 23 '26 15:03

BoarGules



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!