将多个.txt文件作为数据帧加载到python中 [英] loading a multiple .txt files in to python as dataframe

查看:203
本文介绍了将多个.txt文件作为数据帧加载到python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was struggling to load multiple .txt files in to python that are in my desktop. I am totally new to Python. My goal is to load multiple .txt files, which is saved in the same directory. The .txt files are plain texts. Thanks in advance for your help!

推荐答案

您可以执行以下操作.


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"] = file.name
        results["text"].append(file_open.read())
df = pd.DataFrame(results)

这篇关于将多个.txt文件作为数据帧加载到python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆