簡體   English   中英

如何在單獨的文本文件中寫入列表的每個元素?

[英]How to write each element of list in separate text file?

我正在嘗試將列表的每個元素寫入不同的文件中。

假設我們有一個列表:

dataset = ['abc', 'def', 'ghi']

我想遍歷列表並根據列表的長度創建文本文件。 因此,在這種情況下,應該有3個文本文件,每個文本文件分別具有內容abc,def和ghi。

我當前的代碼如下:

# This will read a text file, normalize it and remove stopwords from it using nltk.
import nltk, io, math
from nltk.corpus import stopwords

# Read raw text
targetFile = open('text.txt')
rawtext = targetFile.read()

# Removing stopwords
stops = set(stopwords.words('english'))
filtered_text = [i for i in rawtext.lower().split() if i not in stops]

# Count Number of words
total_words = len(filtered_text)

# Divide them equally into 10 different lists
chunk_size = math.floor(total_words/10)
n_lists_of_words = [filtered_text[i:i + chunk_size] for i in range(0, len(filtered_text), chunk_size)]
if(len(n_lists_of_words) > 10):
    del n_lists_of_words[-1]

# Lets make list of strings instead of list of lists
list_of_str = [' '.join(x) for x in n_lists_of_words]


# Create 10 different files from above 10 elements of n_list_of_words list
for index, word in enumerate(n_lists_of_words):
    with io.FileIO("output_text_" + str(index) + ".txt", "w") as file:
        file.write(bytes(word), 'UTF-8')

錯誤信息:

Traceback (most recent call last):
  File "clean_my_text.py", line 35, in <module>
    file.write(bytes(word), 'UTF-8') 
TypeError: 'str' object cannot be interpreted as an integer

您的代碼只是有點錯誤。 這是更正的最后一行。 file.write(bytes(dataset [count],'UTF-8'))

謝謝你們。 能夠做到這一點。 這是下面的解決方案,請隨時詢問任何相關信息:

# This will read a text file, normalize it and remove stopwords from it using nltk.
import nltk, io, math
from nltk.corpus import stopwords
from string import punctuation

# Read raw text
targetFile = open('input_text.txt')
rawtext = targetFile.read()

# Remove punctuation
def strip_punctuation(s):
    return ''.join(c for c in s if c not in punctuation)
filtered_punc = strip_punctuation(rawtext)
print(filtered_punc)

# Removing stopwords
stops = set(stopwords.words('english'))
filtered_text = [i for i in filtered_punc.lower().split() if i not in stops]

# Count Number of words
total_words = len(filtered_text)

# Divide them equally into 10 different lists
chunk_size = math.floor(total_words/10)
n_lists_of_words = [filtered_text[i:i + chunk_size] for i in range(0, len(filtered_text), chunk_size)]
if(len(n_lists_of_words) > 10):
    del n_lists_of_words[-1]

# Lets make list of strings instead of list of lists
list_of_str = [' '.join(x) for x in n_lists_of_words]

# Print list values in seperate files
for index, word in enumerate(list_of_str):
    with open("Output" + str(index) + ".txt", "w") as text_file:
        print(word, file=text_file)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM