首页 > 解决方案 > 如何使用 Python 3 拆分/解析文本文件(单个列表)的内容并将其写入多个列表?

问题描述

\n假设我在 Python 3 中读取了一个文件(.txt 文件)。接下来,我需要根据原始文件的空格和断句符将单个列表的内容解析为多个列表。

我需要的是分别编写并保存包含列表列表的新文件。

完成此操作后,目录中应该有 2 个文件,一个包含 ,另一个entire text in a single list包含containing only the list of lists.

我已经尝试但尚未成功,感谢任何帮助。

我感到震惊的代码如下。

import nltk, re
import string
from collections import Counter
from string import punctuation
from nltk.tokenize import TweetTokenizer, sent_tokenize, word_tokenize
from nltk.corpus import gutenberg, stopwords
from nltk.stem import WordNetLemmatizer

def remove_punctuation(from_text):
    table = str.maketrans('', '', string.punctuation)
    stripped = [w.translate(table) for w in from_text]
    return stripped

def preprocessing():
    with open("I:\\(2018 - 2019)\\College Desktop\\Pen Drive 8 GB\\PDF\\Code\\Books Handbooks\\Books Handbooks Text\\b1.txt", encoding="utf-8") as f:
         tokens_sentences = sent_tokenize(f.read())
         tokens = [[word.lower() for word in line.split()] for line in tokens_sentences]
         global stripped_tokens
         stripped_tokens = [remove_punctuation(i) for i in tokens]
         sw = (stopwords.words('english'))
    filter_set = [[token for token in sentence if (token.lower() not in sw and token.isalnum() and token.isalpha() and re.findall(r"[^_ .'\"-[A-Za-z]]+", token))] for sentence in stripped_tokens]
    lemma = WordNetLemmatizer()
    lem = []
    for w in filter_set:
        lem.append([wi for wi in map(lemma.lemmatize, w)])
    return lem
result = preprocessing()
with open('I:\\(2018 - 2019)\\College Desktop\\Pen Drive 8 GB\\PDF\\Code\\Books Handbooks\\Books Handbooks Text\\b1_list.txt', "w", encoding="utf-8")  as f1:
    for e in result[:3]:  
        f1.write(str(e))
preprocessing() 

我很沮丧,因为程序执行正确。没有错误,但输出不如预期。例如,在上面的代码中,我希望在新文件中写入前 3 个句子。

但是当我打开新文件时,它显示了 3 个空列表,有点像 [][][]。为什么会这样?

标签: pythonstringpython-3.xlistfile

解决方案


推荐阅读