【问题标题】:read multiple txt files python读取多个txt文件python
【发布时间】:2020-02-28 22:12:12
【问题描述】:

我有 6000 个 txt 文件要在 python 中读取。我正在尝试阅读,但所有 txt 文件都是逐行的。

Subject: key dates and impact of upcoming sap implementation over the next few weeks , project apollo and beyond will conduct its final sap implementation ) this implementation will impact approximately 12 , 000 new users plus all existing system users . sap brings a new dynamic to enron , enhancing the timely flow and sharing of specific project , human resources , procurement , and financial information across business units and across continents . this final implementation will retire multiple , disparate systems and replace them with a common , integrated system encompassing many processes including payroll , timekeeping ...

所以当我一个接一个地读取文件时,python 将它分成几行(我知道那是可笑的)。最后,1 封邮件分成多行。我已经尝试了 read_csv 所有 txt 文件,但 python 给出了 ValueError: stat: path too long for Windows 的错误。我不知道从现在开始我该怎么办。

我试过这个:

import glob
import errno
path =r'C:\Users\frknk\OneDrive\Masaüstü\enron6\emails\*.txt'
files = glob.glob(path)
for name in files:
    try:
        with open(name) as f:
            for line in f:
                print(line.split())
    except IOError as exc:
        if exc.errno != errno.EISDIR:
            raise

['Subject:', 'key', 'dates', 'and', 'impact', 'of', 'upcoming', 'sap', 'implementation']
['over', 'the', 'next', 'few', 'weeks', ',', 'project', 'apollo', 'and', 'beyond', 'will', 'conduct', 'its', 'final', 'sap']

我需要这封电子邮件,但它是逐行分隔的。所以我想要的是每一行都由一封电子邮件表示。

【问题讨论】:

标签: python string pandas


【解决方案1】:

您可以将整个文本文件读入一个变量,然后根据需要进行操作。只需将for line in f 替换为data=f.read() 即可。所以,下面我将每个txt 文件读入数据变量,然后拆分以获取用“”分隔的单词。希望这会有所帮助。

for name in files:
    try:
        with open(name) as f:
            data = f.read().replace("\n","") 
        print(data.split())
    except IOError as exc:
        if exc.errno != errno.EISDIR:
            raise

输出如下所示:

['Subject:', 'key', 'dates', 'and', 'impact', 'of', 'upcoming', 'sap', 'implementationover', 'the', 'next', 'few', 'weeks', ',', 'project', 'apollo', 'and', 'beyond', 'will', 'conduct', 'its', 'final', 'sapimplementation', ')', 'this', 'implementation', 'will', 'impact', 'approximately', '12', ',', '000', 'newusers', 'plus', 'all', 'existing', 'system', 'users', '.', 'sap', 'brings', 'a', 'new', 'dynamic', 'to', 'enron', ',enhancing', 'the', 'timely', 'flow', 'and', 'sharing', 'of', 'specific', 'project', ',', 'human', 'resources', ',procurement', ',', 'and', 'financial', 'information', 'across', 'business', 'units', 'and', 'acrosscontinents', '.this', 'final', 'implementation', 'will', 'retire', 'multiple', ',', 'disparate', 'systems', 'and', 'replacethem', 'with', 'a', 'common', ',', 'integrated', 'system', 'encompassing', 'many', 'processes', 'includingpayroll', ',', 'timekeeping', '...']```

【讨论】:

  • 可能值得一提的是data = f.read()替换了for line in f
  • 成功了,谢谢。我想要的是从现在开始,每个文件的文件名中都有“垃圾邮件”或“火腿”文本。所以我想用文件内容来获取它们。例如:1. column 表示 ham 2. column 表示同一行的 email。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
  • 1970-01-01
  • 2017-04-21
  • 2016-10-24
相关资源
最近更新 更多