читать несколько текстовых файлов python - PullRequest
0 голосов
/ 29 февраля 2020

У меня есть 6000 текстовых файлов для чтения в python. Я пытаюсь прочитать, но все текстовые файлы построчно.

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 все текстовые файлы, но 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']

Мне нужно это письмо по электронной почте, но оно отделялось построчно. Так что я хочу, чтобы каждая строка была представлена ​​одним письмом.

1 Ответ

0 голосов
/ 29 февраля 2020

Вы можете прочитать весь текстовый файл в переменную и в дальнейшем манипулировать, как хотите. Просто замените for line in f на data=f.read(). Так, ниже я читаю каждый текстовый файл в переменную данных, а потом разделяю, чтобы слова отделялись "". Надеюсь, это поможет.

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', '...']```
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...