简体   繁体   中英

list index out of range when appending data in CSV file

I have a CSV file in which I have data in three columns and I want to add new rows after checking the data exists then want to add these data in new rows but getting the error list index out of range

this is my code

Categories

name
apple
banana
potatoes
onion

CSV data

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()

Note: Data is added in the existing CSV file but CSV file writing with a blank line between every loop.

The main issue with the original solution was handling the file. The outer for loop wouldn't stop, since the inner for loop was appending line to the working file.

Here, I used a simple list to store what needs to be duplicated. If the file is too large, another option would be to use a second CSV file as a buffer and then copy it back at the end.

Also, consider using 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM