below code! however, when run name not found? (it in csv file howevever)
import csv name = "andy" score = "53" x = 0 array = [] f = open('class a.csv', 'r') csv_file = csv.reader(f) row in csv_file: if row[0] == [name]: print("found name") row[1].append(score) f.close() break else: print("not found") f = open('class a.csv', 'a') = csv.writer(f) array.append(name) array.append(score) print(array) a.writerow(array) f.close() break this csv file:
steve,3 max,2 dave,5 andy,3 my second issue above code if wrote 2 different names there blank line between them.
the program printed not found because if first comparing didn't matched printed not found, need iterate names , if there no match then, insert new row. intention ?
import csv name = "andy" score = "53" x = 0 array = [] f = open('class a.csv', 'r') csv_file = csv.reader(f) is_found = false row in csv_file: if row[0] == name: print("found name") is_found = true row.append(score) f.close() break if not is_found: print("not found") f = open('class a.csv', 'a') = csv.writer(f) array.append(name) array.append(score) print(array) a.writerow(array) f.close()
Comments
Post a Comment