I am reading in a csv file to a dictionary, transforming the data as needed, and writing it to a new csv file.
The original csv file has a column where some strings (words) are in double quotation marks while other strings are not in quotation marks.
Like this:
FOODS;CALS
"PIZZA";600
"PIZZA";600
"BURGERS";500
"PIZZA";600
PASTA;400
"PIZZA";600
SALAD;100
CHICKEN WINGS;300
"PIZZA";600
"PIZZA";600
After I write this column to my output file, it looks like the array below, where the words from the original CSV that were in quotation marks now have three quotations around them and others have none:
FAVORITE_FOOD;VOTES
"""PIZZA""";6
"""BURGERS""";1
PASTA;1
SALAD;1
CHICKEN WINGS;1
I need to remove the quotation marks so my final csv looks like this:
FAVORITE_FOOD;VOTES
PIZZA;6
BURGERS;1
PASTA;1
SALAD;1
CHICKEN WINGS;1
Here is how I'm reading in the file:
with open(input_data_txt, "r") as file:
# This enables skipping the header line.
skipped = islice(file, 1, None)
for i, line in enumerate(skipped, 2):
try:
food, cals = line.split(';')
except ValueError:
pass
And here is how I'm writing it:
with open(food_txt, 'w') as myfile:
wr = csv.writer(myfile, delimiter=';')
for i in final_array:
wr.writerow(i)
The triple quotes are likely added by the csv
module to escape the existing quotes.
So instead of something like:
csvwriter.writeline(food, vote)
Try something like:
csvwriter.writeline(food.strip('"'), vote)