Question

Il y a beaucoup de postes concernant l'analyse Twitter JSON, mais aucun que j'ai vu résolu mon problème.

Ceci est code

import json

file = open('tweet', 'r')
tweet = file.read()
#{"geo":null,"text":"Lmao!! what time? I dont finish evening cleaning till 5 RT \u201c@some_user: football anyone?.....i wanna have a kickabout :(\u201d"}
#{"geo":null,"text":"Lmao!! what time? I dont finish evening cleaning till 5 RT @some_user: football anyone?.....i wanna have a kickabout :("}
def parseStreamingTweet(tweet):
    try:
        singleTweetJson = json.loads(tweet)
        for index in singleTweetJson:
            if index == 'text':
                print "text : ", singleTweetJson[index]
    except ValueError:
        print "Error ", tweet
        print ValueError
        return

parseStreamingTweet(tweet)

C'est le programme de test.Tweet est livré dans le flux et à des fins de vérification, j'ai enregistré un tweet dans un fichier et vérifié.Il y a une partie éditée de l'alimentation Twitter.

Quelqu'un peut-il me dire comment analyser le tweet qui sont uni-codé.Le premier tweet dans le commentaire est uni-codé et le second n'est pas.Il y a d'une erreur en premier, tout en supprimant la chaîne uni-code, l'analyse est réussie.Que peut être la solution?

Était-ce utile?

La solution

I think your code works, the reason for the error is probably because of a UnicodeEncodeError which happens when you try to print the unicode value to the terminal. I'm guessing you are calling the script in a non-unicode aware terminal. If instead you printed the repr of the unicode value, or (wrote it to an output file) it would probably work:

print "text : ", repr(singleTweetJson[index])

Also its generally bad practice to hide specific exceptions/error messages with generic catch-all exceptions/error messages.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top