我目前有一个文件夹,其中包含多个名称相似的文件,我正在尝试从中读取这些文件。 例如: 文件夹包含文件:
apple_2019_08_26_23434.xls
apple_2019_08_25_55345.xls
apple_2019_08_24_99345.xls
文件的名称格式很简单:
apple_<date>_<5 random numbers>.xls
如果我不关心末尾的随机 5 位数字,如何将 excel 文件读入 pandas df?
例如
df = pd.read_excel('e:\Document\apple_2019_08_26_<***wildcard***>.xls')
谢谢!
请您参考如下方法:
您可以通过 glob 使用 unix 风格的路径名扩展.
import glob
# get .txt files in current directory
txt_files = glob.glob('./*.txt')
# get .xls files in some_dir
xls_files = glob.glob('some_dir/*.xls')
# do stuff with files
# ...
这里,* 基本上表示“任何东西”。
Pandas 的例子:
import glob
for xls_file in glob.glob('e:/Document/apple_2019_08_26_*.xls'):
df = pd.read_excel(xls_file)
# do stuff with df
# ...
