尝试从JSON中提取Extended_Tweet。这是我正在使用的代码。它一直给我错误。我该如何做得更好?
def process(inputfn, outputfn):
report = []
for line in open(inputfn):
ob = json.loads(line)
try:
data = tweet['extended_tweet']['full_text'] "").replace("\n", " ").replace(" ", " ").strip()
except KeyError as e:
print(e)
data = tweet['stuff']
user = ob.get("user")
username = "unknown"
if user:
username = user.get("screen_name", "")
msg = u"%s: %s" % (username, text)
report.append(msg)
with codecs.open(outputfn, "w", "utf8") as f:
f.write("\n".join(report))
if __name__ == "__main__":
process("data/test.json", "data/stream_test-part-tweets.txt")
修改:链接到pastebin:https://pastebin.com/KTHwD5E5 新编辑:添加了更新的代码:链接:https://pastebin.com/Uf0Nb3bg