从python中读取多个txt文件
我有6000个txt文件要用python读取。我正在尝试读取,但所有的txt文件都是逐行读取的从python中读取多个txt文件,python,string,pandas,Python,String,Pandas,我有6000个txt文件要用python读取。我正在尝试读取,但所有的txt文件都是逐行读取的 主题:即将实施的sap的关键日期和影响 在接下来的几周里,阿波罗计划和其他计划将进行最后的sap 实施)该实施将影响大约12000个新项目 用户加上所有现有系统用户。sap为安然带来了新的活力, 加强具体项目、人力资源的及时流动和共享, 跨业务部门和跨部门的采购和财务信息 大洲。 这一最终实现将淘汰多个不同的系统,并取代 他们有一个共同的、集成的系统,包含许多过程,包括 工资单,计时。。。 所以,当
主题:即将实施的sap的关键日期和影响
在接下来的几周里,阿波罗计划和其他计划将进行最后的sap
实施)该实施将影响大约12000个新项目
用户加上所有现有系统用户。sap为安然带来了新的活力,
加强具体项目、人力资源的及时流动和共享,
跨业务部门和跨部门的采购和财务信息
大洲。
这一最终实现将淘汰多个不同的系统,并取代
他们有一个共同的、集成的系统,包含许多过程,包括
工资单,计时。。。
所以,当我一个接一个地读取文件时,python会将其分隔成行(我知道这很可笑)。最后,一封邮件被分成多行。我尝试了read\u csv
所有的txt文件,但python给出的错误是ValueError:stat:path对于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']
我需要这封电子邮件,但它是一行一行分开的。因此,我想要的是每一行由一封电子邮件表示。您可以将整个文本文件读入一个变量,然后根据需要进行操作。只需将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()。我想要的是,从现在起,每个文件的文件名中都有“垃圾邮件”或“火腿”文本。所以我想用文件的内容来获取它们。例如:1。专栏说火腿2。同一行中的列表示其电子邮件。