如何将具有两个变量的字符串数组放入文本文件中?

How to put an array of strings with two variables into a text file?

我正在尝试制作一个文本分类器应用程序。我有一个字符串数组,其中包含两个用逗号分隔的参数,如下所示:

pos_tweets = [('I love this car', 'positive'),
              ('This view is amazing', 'positive'),
              ('I feel great this morning', 'positive')]

使用该字符串数组,我可以执行以下代码:

tweets = []
for (words, sentiment) in pos_tweets:
    words_filtered = [e.lower() for e in words.split() if len(e) >= 3] 
    tweets.append((words_filtered, sentiment))
print(tweets)

输出:

[(['love', 'this', 'car'], 'positive'), (['this', 'view', 'amazing'], 'positive'), (['feel', 'great', 'this', 'morning'], 'positive')]

我想做的是将该字符串数组放入一个文本文件中,并且仍然能够执行具有与上述相同输出的代码。

您可以有一个文本文件,其中每一行都包含 pos_tweets 数组中的一个元素。所以在

pos_tweets.txt

I love this car, positive
This view is amazing, positive

然后你可以在每一行中阅读

import csv

tweets = []
with open('pos_tweets.txt') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        words = row[0]
        sentiment = row[1]
        words_filtered = [e.lower() for e in words.split() if len(e) >= 3] 
        tweets.append((words_filtered, sentiment))

要将您的列表输出到 txt 文件,您可以使用 pickle 模块

我看不出你如何在读取 txt 文件后获得相同的输出,但你可以在当前代码中进行非常小的修改。

要将列表输出到文本文件,你可以这样做:

with open('outfile', 'wb') as fp:
    pickle.dump(tweets, fp)

要阅读它,您可以这样做:

with open('outfile', 'rb') as fp:
    pos_tweets = pickle.load(fp)

for (words, sentiment) in pos_tweets:
    if type(words) == list:
        words = ' '.join(words)
    words_filtered = [e.lower() for e in words.split() if len(e) >= 3]
    tweets.append((words_filtered, sentiment))

随着上述修改后的代码迭代到 pos_tweets,您可以使用硬编码推文或从 txt 文件中读取的推文。

完整代码如下:

import pickle

pos_tweets = [('I love this car', 'positive'),
              ('This view is amazing', 'positive'),
              ('I feel great this morning', 'positive')]


def get_tweets(p_tweets):
    tweets = []
    for (words, sentiment) in p_tweets:
        if type(words) == list:
            words = ' '.join(words)
        words_filtered = [e.lower() for e in words.split() if len(e) >= 3]
        tweets.append((words_filtered, sentiment))
    return tweets


t = get_tweets(pos_tweets)

print(t)
with open('outfile', 'wb') as fp:
    pickle.dump(t, fp)

with open('outfile', 'rb') as fp:
    pos_tweets = pickle.load(fp)

t = get_tweets(pos_tweets)
print(t)