擅长:python、mysql、java
<p>使用pathlib,我们可以创建一个生成器表达式,然后创建一个以文件名为名称、以值为数据帧的字典</p>
<p>使用pathlib,我们可以使用<code>.glob</code>模块获取目标路径中的所有csv</p>
<p>如果您使用的windows使用原始字符串或转义斜杠,请将“\tmp\files”替换为文件的路径</p>
<pre><code>from pathlib import Path
trg_files = (f for f in Path("\tmp\files").glob("*.csv"))
dataframe_dict = {f"{file.stem}_df": pd.read_csv(file) for file in trg_files}
print(dataframe_dict.keys())
'company_df'
print(datarame_dict['company_df'])
</code></pre>