파이썬으로 json 파일 형식 (retweet 제외)의 트윗에 텍스트를 추출하는 파이썬 프로그램을 작성하려고합니다. 다음은 파이썬에서 snippcode입니다 (파일은 20MB이므로 여기에 포함되지 않습니다).짹짹 구문 분석에서 json 형식의 모든 텍스트를 읽을 수 없습니까?
import sys
import difflib
import twitter
import json
from pprint import pprint
# Input argument is the filename of the JSON ascii file from the Twitter API
filename = sys.argv[1]
tweets_text = [] # We will store the text of every tweet in this list
tweets_location = [] # Location of every tweet (free text field - not always `enter code here`accurate or given)
tweets_timezone = [] # Timezone name of every tweet
# Loop over all lines
f = file(filename, "r")
lines = f.readlines()
for line in lines:
try:
tweet = json.loads(line)
# Ignore retweets!
if (tweet[1].has_key('retweeted_status') or not (tweet[1].has_key('text'))):
continue
# Fetch text from tweet
text = tweet[1]['text'].encode('utf-8','ignore').lower()
# Ignore 'manual' retweets, i.e. messages starting with RT
if text.find("RT ") > -1:
continue
tweets_text.append(text)
tweets_location.append(tweet[1]['user']['location'].encode('utf-8','ignore'))
tweets_timezone.append(tweet[1]['user']['time_zone'].encode('utf-8','ignore'))
except ValueError:
pass
# Show result
print tweets_text
문제는 단지 하나의 트윗을 얻는 것입니다. 누구든지 오류를 지적 할 수 있습니까?
'ValueError' 예외를 포착하지 않는 것으로 시작하십시오. JSON 데이터는 여러 행의 데이터에 걸쳐있을 수 있습니다. –
질문의 형식은 유감 스럽다. 이 오류를 해결하는 방법을 알려주십시오. 나는 다른 포스트에서 가져온 코드 다음 사용하면 이후 : 수입 JSON pprint 수입 pprint에서 json_data = 개방 ('data.json') 데이터 = json.load (json_data) pprint (데이터) 내가 볼 수 있었다 공헌자, 좌표, ID, .., 텍스트와 같은 많은 내용 ... – user1993617