3

I have a list of items:

List = ['apple', 'banana', 'orange'] 

and a csv file:

apple kiwi banana orange pear 

I am trying to see for each item in the list if it matches the first column of the csv, and if it does, append a '1' to the end of the row (otherwise appending a '0'), but I am a bit lost. The code below is pretty much what I'm trying to get the end result to be.

apple, 1 kiwi, 0 banana, 1 orange, 1 pear, 0 

I am very new to python and would really appreciate any help! Thanks

0

4 Answers 4

1

I tried below code, which will store result in CSV

import csv lists = ['apple', 'banana', 'orange'] nf = csv.writer(open("output.csv", "w")) with open('input.csv', 'rb') as f: reader = csv.reader(f) for row in reader: if(''.join(row) in lists): nf.writerow([''.join(row),1]) else: nf.writerow([''.join(row),0]) 

OUTPUT in CSV

apple,1 kiwi,0 banana,1 orange,1 pear,0 
Sign up to request clarification or add additional context in comments.

1 Comment

@hsifyllej Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
1

Perhaps something like this. It'll read from the csv, count how many times each fruit is found in your list, then overwrite the csv file with the new data.

import csv List = ['apple', 'banana', 'orange'] data = [] filename = 'file.csv' with open(filename, newline='') as f: reader = csv.reader(f) for row in reader: data.append(row) for item in List: for fruit in data: if item in fruit: fruit[1] = int(fruit[1]) + 1 with open(filename, 'w', newline='') as f: writer = csv.writer(f) writer.writerows(data) 

Comments

1

Note that your csv file is rather a plain text file. But also a valid csv, so I'll treat it like one.

Read line by line and print 0 or 1 alongside the data using a ternary expression. Quick & easy, no dictionary or counters needed and preserves the order:

import csv List = {'apple', 'banana', 'orange'} with open("csv.csv") as f: cr = csv.reader(f) for row in cr: print("{}, {}".format(row[0],1 if row[0] in List else 0)) 

since it's not really a csv, basic line by line read will do too:

List = {'apple', 'banana', 'orange'} with open("csv.csv") as f: for item in f: item = item.strip() print("{}, {}".format(item,1 if item in List else 0)) 

note that in both examples I have used a set, not a list. Makes a difference speed-wise when there are a lot of items.

Comments

0

You can easily achieve this using the csv module in the Python standard library, specifically the csv.reader() method.

import csv fruit_list = ['apple', 'banana', 'orange'] fruit_in_file = {} with open('csv_file.csv', 'r') as csvfile: fruit_reader = csv.reader(csvfile) for row in fruit_reader: fruit = row[0] if fruit in fruit_list: fruit_in_file[fruit] = 1 else: fruit_in_file[fruit] = 0 print(fruit_in_file) >>> {'apple': 1, 'kiwi': 0, 'orange': 1, 'banana': 1, 'pear': 0} 

After achieving this, it seems from your question that you want to write a csv file containing this data, so we'll iterate through our dict and write to a csv file using the csv.writer() method.:

with open('new_csv_file.csv', 'w') as csvfile: fruit_writer = csv.writer(csvfile) for key, value in fruit_in_file.items(): fruit_writer.writerow([key, value]) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.