I would like to append an empty list "city_names" and add all the names of a city in order from an excel file I have imported.
city_names = []
for city in cities["City"]:
print("city")
print(city_names)`
should give me this result:
['Buenos Aires',
'Toronto',
'Pyeongchang',
'Marakesh',
'Albuquerque',
'Los Cabos',
'Greenville',
'Archipelago Sea',
'Walla Walla Valley',
'Salina Island',
'Solta',
'Iguazu Falls']
I am not sure why it doesn't work. The filename is 'cities'.
Hope this snippet may help you.
import xlrd
workbook = xlrd.open_workbook('Cities.xlsx') # put your filename
worksheet = workbook.sheet_by_name('Cities') # put sheet name
cities=[c.value for c in worksheet.col(0)] # expecting cities are in first column
If you are using panads:
import pandas as pd
filePath = 'cities.xlsx' #your path to cities
df = pandas.read_excel(filePath) #df = pandas.read_csv(filePath) if your file is in CSV format
city_names = df['city'].tolist()
You can now iterate through this list.
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.