gpt4 book ai didi

python - 将多个 .txt 文件作为数据框加载到 python 中

转载 作者:太空宇宙 更新时间:2023-11-04 01:48:58 27 4
gpt4 key购买 nike

我正在努力将多个 .txt 文件加载到我桌面上的 python 中。我是 Python 的新手。我的目标是加载多个 .txt 文件,这些文件保存在同一目录中。 .txt 文件是纯文本。在此先感谢您的帮助!

最佳答案

你可以这样做。


from collections import defaultdict
from pathlib import Path
import pandas as df

my_dir_path = "/parh/to/folder"

results = defaultdict(list)
for file in Path(my_dir_path).iterdir():
with open(file, "r") as file_open:
results["file_name"].append(file.name)
results["text"].append(file_open.read())
df = pd.DataFrame(results)

关于python - 将多个 .txt 文件作为数据框加载到 python 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58467374/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com