I'm looping through a CSV, and would like to change "Gotham" to "Home". I've tried a couple ways, after searching around online, but can't seem to get it to work.
import csv
csv_file = "test.csv"
def process_csv(file):
headers=[]
data = []
csv_data = csv.reader(open(file))
for i, row in enumerate(csv_data):
if i == 0:
headers = row
continue;
field = []
for i in range(len(headers)):
field.append((headers[i],row[i]))
data.append(field)
return data
def create_merge_fast(city, country, contact):
lcl = locals()
## None of these do what I'd think - if city is "Gotham" change it to "Home"
for key, value in lcl.items():
if value == "Gotham":
lcl[value] = "Home"
print(key, value)
for value in lcl.values():
if value == "Gotham":
lcl[value] = "Home"
print(value)
def set_fields_create_doc(data):
city = data[4][1]
country = data[6][1]
contact = data[9][1]
create_merge_fast(city, country, contact)
data = process_csv(csv_file)
for i in data:
set_fields_create_doc(i)
I always seem to get
RuntimeError: dictionary changed size during iteration
right after
Gotham
is printed...