TypeError:'in' 左侧操作数需要字符串,而不是生成器

1 投票
1 回答
5568 浏览
提问于 2025-04-17 03:32

我正在尝试解析推特数据。

我的数据结构如下:

59593936 3061025991 null null <d>2009-08-01 00:00:37</d> <s>&lt;a href="http://help.twitter.com/index.php?pg=kb.page&amp;id=75" rel="nofollow"&gt;txt&lt;/a&gt;</s> <t>honda just recalled 440k accords...traffic around here is gonna be light...win!!</t> ajc8587 15 24 158 -18000 0 0 <n>adrienne conner</n> <ud>2009-07-23 21:27:10</ud> <t>eastern time (us &amp; canada)</t> <l>ga</l>
22020233 3061032620 null null <d>2009-08-01 00:01:03</d> <s>&lt;a href="http://alexking.org/projects/wordpress" rel="nofollow"&gt;twitter tools&lt;/a&gt;</s> <t>new blog post: honda recalls 440k cars over airbag risk http://bit.ly/2wsma</t> madcitywi 294 290 9098 -21600 0 0 <n>madcity</n> <ud>2009-02-26 15:25:04</ud> <t>central time (us &amp; canada)</t> <l>madison, wi</l>

我想要获取推文的总数以及与关键词相关的推文数量。我已经在文本文件中准备好了关键词。此外,我还想获取推文的文本内容,以及包含提及(@)、转发(RT)和网址的推文总数(我想把每个网址保存到另一个文件中)。

所以,我写了这样的代码。

import time
import os

total_tweet_count = 0
related_tweet_count = 0
rt_count = 0
mention_count = 0
URLs = {}

def get_keywords(filepath, mode):
    with open(filepath, mode) as f:
        for line in f:
            yield line.split().lower()

for line in open('/nas/minsu/2009_06.txt'):
    tweet = line.strip().lower()

    total_tweet_count += 1

    with open('./related_tweets.txt', 'a') as save_file_1:
        keywords = get_keywords('./related_keywords.txt', 'r')

        if keywords in line:
            text =  line.split('<t>')[1].split('</t>')[0]

            if 'http://' in text:
                try:
                    url = text.split('http://')[1].split()[0]
                    url = 'http://' + url

                    if url not in URLs:
                        URLs[url] = []
                    URLs[url].append('\t' + text)

                    save_file_3 = open('./URLs_in_related_tweets.txt', 'a')
                    print >> save_file_3, URLs

                except:
                    pass

            if '@' in text:
                mention_count +=1

            if 'RT' in text:
                rt_count += 1

            related_tweet_count += 1

            print >> save_file_1, text

    save_file_2 = open('./info_related_tweets.txt', 'w')

print >> save_file_2, str(total_tweet_count) + '\t' + srt(related_tweet_count) + '\t' + str(mention_count) + '\t' + str(rt_count)

save_file_1.close()
save_file_2.close()
save_file_3.close()

以下是一些示例关键词:

Depression
Placebo
X-rays
X-ray
HIV
Blood preasure
Flu
Fever
Oral Health
Antibiotics
Diabetes
Mellitus
Genetic disorders

我觉得我的代码有很多问题,但第一个错误是这样的:

追踪(最近的调用最后):文件 "health_related_tweets.py",第23行,if keywords in line: TypeError: 'in ' 需要字符串作为左操作数,而不是生成器

请帮帮我!

1 个回答

2

原因是 keywords = get_keywords(...) 返回的是一个生成器。简单来说,生成器就像一个可以逐个产生值的机器,而不是一次性给你所有的值。我们本来想要的是一个包含所有关键词的列表。然后,对于这个列表里的每一个关键词,我们都想检查它是否出现在推文或行文本中。

示例代码:

keywords = get_keywords('./related_keywords.txt', 'r')
has_keyword = False
for keyword in keywords:
  if keyword in line:
    has_keyword = True
    break
if has_keyword:
  # Your code here (for the case when the line has at least one keyword)

(上面的代码将替代 if keywords in line:

撰写回答