python - 如何使用 Python 遍历 excel 文件
问题描述
我想附加一个空列表“city_names”,并从我导入的 excel 文件中按顺序添加一个城市的所有名称。
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']
我不确定为什么它不起作用。文件名是“城市”。
解决方案
希望这个片段可以帮助你。
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
推荐阅读
- python - 在 crom 中执行时无法让我的 python 脚本工作/播放音频
- python - 如何在窗口上使用 collect_list 在 Pyspark 中创建嵌套列表?
- reactjs - 如何从我的资产文件夹中动态导入图像?
- sharepoint - 基于 Sharepoint 列表中的第三列的查找值
- reactjs - 如何显示来自 React-Query 的单个缓存数据?
- python - 使用 selenium、bs4 或请求从交互式图表中抓取数据
- azure - Docker:“在 Azure 上部署 Docker 容器”说明
- pine-script - Pinescript 条件线绘图
- pandas - 如何更改 cartopy 地图中颜色条的类型?
- python - 如何通过使用 Python 对特定列进行分组来求和?