【问题标题】:How to iterate thru different filenames in python如何在python中遍历不同的文件名
【发布时间】:2020-07-05 16:30:25
【问题描述】:

我已经为变量分配了不同的文件。现在我想做一些迭代这些变量的操作。例如: reduced_file1= 'names.xlsx' reduced_file2= 'surnames.xlsx' reduced_file3= 'city.xlsx' reduced_file4= 'birth.xlsx'

我想要迭代的操作(使用 FOR 循环)是:

xls= pd.ExcelFile(reduced_file1) xls= pd.ExcelFile(reduced_file2) xls= pd.ExcelFile(reduced_file3) xls= pd.ExcelFile(reduced_file4) ...等等

基本上每次都是改变量名:reduce_file(i)

谢谢

【问题讨论】:

  • 这远低于stackoverflow问题的水平

标签: python pandas jupyter-notebook


【解决方案1】:
files= ['names.xlsx', 'surnames.xlsx', 'city.xlsx', 'birth.xlsx']

for file in files:
    xls = pd.ExcelFile(file)

您还可以使用 f-strings 更改字符串名称:

for i in range(4):
    print(f"this is number {i}")

【讨论】:

  • 非常感谢大家!!
猜你喜欢
  • 1970-01-01
  • 2011-08-09
  • 2016-02-07
  • 1970-01-01
  • 2019-09-03
  • 2018-12-26
  • 1970-01-01
  • 2021-11-22
  • 1970-01-01
相关资源
最近更新 更多