pandas 使用部分通配符读取csv文件 [英] Pandas reading csv files with partial wildcard
问题描述
我正在尝试编写一个脚本,该脚本可以导入文件,然后对该文件执行某些操作,然后将结果输出到另一个文件中.
I'm trying to write a script that imports a file, then does something with the file and outputs the result into another file.
df = pd.read_csv('somefile2018.csv')
上面的代码工作得很好.但是,我想避免对代码中的文件名进行硬编码.
The above code works perfectly fine. However, I'd like to avoid hardcoding the file name in the code.
该脚本将在包含script.py
和多个csv文件的文件夹(目录)中运行.
The script will be run in a folder (directory) that contains the script.py
and several csv files.
我尝试了以下操作:
somefile_path = glob.glob('somefile*.csv')
df = pd.read_csv(somefile_path)
但是出现以下错误:
ValueError: Invalid file path or buffer object type: <class 'list'>
推荐答案
glob
返回列表,而不是字符串. read_csv
函数将字符串作为输入来查找文件.试试这个:
glob
returns a list, not a string. The read_csv
function takes a string as the input to find the file. Try this:
for f in glob('somefile*.csv'):
df = pd.read_csv(f)
...
# the rest of your script
这篇关于 pandas 使用部分通配符读取csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!