트윗 데이터를 구문 분석하려고합니다.python tweet parsing
59593936 3061025991 null null <d>2009-08-01 00:00:37</d> <s><a href="http://help.twitter.com/index.php?pg=kb.page&id=75" rel="nofollow">txt</a></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 & canada)</t> <l>ga</l>
22020233 3061032620 null null <d>2009-08-01 00:01:03</d> <s><a href="http://alexking.org/projects/wordpress" rel="nofollow">twitter tools</a></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 & canada)</t> <l>madison, wi</l>
내가 트윗의 총 수와 키워드 관련 트윗의 수를 얻으려면 다음과 같이
내 데이터 모양이다. 텍스트 파일에 키워드를 준비했습니다. 또한 트윗 텍스트 내용, 언급 (@), 리트 윗 (RT) 및 URL (다른 파일의 모든 URL을 저장하고 싶음)이 포함 된 트윗의 총 수를 얻고 싶습니다.
이렇게 코드를 작성했습니다.
import time
import os
total_tweet_count = 0
related_tweet_count = 0
rt_count = 0
mention_count = 0
URLs = {}
def get_keywords(filepath):
with open(filepath) as f:
for line in f:
yield line.split()
for line in open('/nas/minsu/2009_06.txt'):
tweet = line.strip()
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()
키워드 세트는
Happy
Hello
Together
내가 내 코드는 많은 문제를 가지고 있다고 생각 좋아하지만, 첫 번째 오류는 follws과 같습니다
Traceback (most recent call last):
File "health_related_tweets.py", line 21, in <module>
keywords = get_keywords('./public_health_related_words.txt', 'r')
TypeError: get_keywords() takes exactly 1 argument (2 given)
저를 도와주세요!
역 추적 (마지막으로 가장 최근 통화) : 파일 "health_related_tweets.py", 라인 23에서 키워드의 경우 TypeError : 'in '은 생성자가 아닌 왼쪽 피연산자로 문자열을 필요로합니다. /// 이제이 오류가 발생했습니다. PLZ 도와주세요! –
ooozooo
@MINSUPARK'get_keywords()'는 문자열이 아닌 생성자를 반환하므로'keyword in line :'을 호출하면'keywords'가 문자열이 아니기 때문에 오류가 발생합니다. –
@CodyHess 그러면 어떻게 해결할 수 있습니까? 사실, 나는 초보자이다. 그래서 네 도움이 필요해! – ooozooo