引号出现在一些字符串中,而另一些则没有。如何使所有字符串都相同?

Quotation marks appear with some strings and not others. How can I make all strings the same?

我正在将 csv 文件读入字典,根据需要转换数据,然后将其写入新的 csv 文件。

原始csv文件有一列,其中一些字符串(单词)用双引号引起来,而另一些字符串没有用引号引起来。

像这样:

FOODS;CALS
"PIZZA";600
"PIZZA";600
"BURGERS";500
"PIZZA";600
PASTA;400
"PIZZA";600
SALAD;100
CHICKEN WINGS;300
"PIZZA";600
"PIZZA";600

在我将此列写入输出文件后,它看起来像下面的数组,其中来自原始 CSV 中的引号中的单词现在有三个引号,其他的有 none:

FAVORITE_FOOD;VOTES
"""PIZZA""";6
"""BURGERS""";1
PASTA;1
SALAD;1
CHICKEN WINGS;1

我需要删除引号,以便我的最终 csv 如下所示:

FAVORITE_FOOD;VOTES
PIZZA;6
BURGERS;1
PASTA;1
SALAD;1
CHICKEN WINGS;1

以下是我在文件中的阅读方式:

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

我是这样写的:

with open(food_txt, 'w') as myfile:
    wr = csv.writer(myfile, delimiter=';')
    for i in final_array:
        wr.writerow(i)

三重引号可能是由 csv 模块添加以转义现有引号。

所以不要像这样:

csvwriter.writeline(food, vote)

试试这样的东西:

csvwriter.writeline(food.strip('"'), vote)

使用replace:

file = open(r"PY.csv")
text = file.read()
text = text.split()
for i in range(len(text)):
    text[i] = text[i].replace('"',"")

你会得到这样的列表:

PIZZA
PIZZA
BURGERS
PIZZA
PASTA
PIZZA
SALAD
CHICKEN WINGS
PIZZA
PIZZA

您可以使用 csv.DictReader,这样您就可以按名称对列进行寻址,collections.Countercount 每种食物出现的次数,然后使用 csv.writer 相应地输出它们,例如:

import csv
from collections import Counter

with open('input_file') as fin, open('output_file', 'wb') as fout:
    # Count occurrences of each FOODS type
    votes = Counter(row['FOODS'] for row in csv.DictReader(fin, delimiter=';'))
    # Create a csv.writer around the output file and write the header columns
    csvout = csv.writer(fout, delimiter=';')
    csvout.writerow(['FAVORITE_FOOD', 'VOTES'])
    # Write the name and vote counts to the file
    csvout.writerows(votes.items())