I need a small help....
Csv file sample :
ID, name, age, city1,city2,city3
1, Andy, 25, "Ann Arbor,NA","CA,NA","MS,NA"
2, Bella, 40, "Los Angeles,NA"
3, Cathy, 13, "Eureka,NA","NV,NA"
My current code :
name=[]
age=[]
with open ('File_name','r') as f:
reader=DictReader(f)
for row in reader:
name.append(row['name'])#append all names
age.append(row['age'])
Now i need to print the cities.There is no certainity that will lived in only 3 cities.While updating data source,there might be more cities....So what i think is Creating a variables by loop .
Method i tried : Our Column is 3...there is no changes in it..
ID=2 #user requirement
name=[]
age=[]
cities=[]
with open ('File_name','r') as f:
reader=DictReader(f)
for row in reader:
if ID == row['ID']:
name.append(row['name'])#append all names
age.append(row['age'])
Def_Fil=len(row)-3
for i in range(Def_Fil):
city=city.append(row['city+str(i)']) #I dont aware how to declare the row name,i need to iterate manually...i can print name and age...but here depend number of cities i need to declare.
print(name,age,city)
But am facing "SyntaxError: cannot assign to operator"
My expected output:
when i print city of ID 3 : ["Eureka,NA","NV,NA"]
ID 2 : ["Los Angeles,NA"]
ID 1 : ["Ann Arbor,NA","CA,NA","MS,NA"]