在 CSV 文件中追加数据时列表索引超出范围

list index out of range when appending data in CSV file

我有一个 CSV 文件,其中有三列数据,我想在检查数据存在后添加新行,然后想在新行中添加这些数据,但出现错误 list index out of range

这是我的代码

类别

name
apple
banana
potatoes
onion

CSV 数据

titles,summaries,terms
apple,,Apple (Fruit)
banana,,Banana (Fruit)
potatoes,,Potato (Vegitable)
onion,,Onion (Vegitable)    
categories = db.table('categories').get()

csv_data = csv.reader(open('categories.csv', "r"))

csv_matched_strings = []
for row in csv_data:
    for category in categories:
        if category.name in row[0]:
            print(row[0] + ': ' + row[2])
            csv_matched_strings.append(category.name)
            List = [row[0],'',row[2]]
            with open('categories.csv', 'a+') as f_object:
                writer_object = writer(f_object)
                writer_object.writerow(List)
                f_object.close()

注意:数据添加到现有的 CSV 文件中,但 CSV 文件在每个循环之间写入一个空行。

原始解决方案的主要问题是处理文件。外部 for 循环不会停止,因为内部 for 循环正在将行附加到工作文件。

在这里,我使用了一个简单的列表来存储需要复制的内容。如果文件太大,另一种选择是使用第二个 CSV 文件 作为缓冲区,然后在最后将其复制回来。

此外,考虑使用 sets when doing look-ups and maybe learn about how the in operator works here is a post you might find helpful

import csv
from csv import writer

# Since OP hasn't provided the class, consider this a substitute
categories = [
    {'name': 'apple'},
    {'name': 'banana'},
    {'name': 'potatoes'},
    {'name': 'onion'},
]

# set of category names created using a set comprehension
cat = {c['name'] for c in categories}

found_rows = []
csv_matched_strings = []

with open('categories.csv', "r") as f:
    csv_data = csv.reader(f)

    for row in csv_data:
        if row[0] in cat:
            # First value is found in categories
            print(row[0] + ': ' + row[2])
            found_rows.append([row[0], '', row[2]])
            # I left it here since it was there in the original code
            csv_matched_strings.append(row[0])

# categories.csv is now closed.
# We open it again in append mode and proceed to append the duplicates
with open('categories.csv', 'a+') as f:
    writer_object = writer(f)
    for row in found_rows:
        writer_object.writerow(row)