I am trying to stream arabic tweet from an api twitter with python









up vote
1
down vote

favorite












class listener(StreamListener):
def on_data(self, data):
try:
tweet=data
t=tweet.encode('utf-8').decode('unicode_escape')
print(t)
saveFile = open('E:\da.json','a')
saveFile.write(t)
saveFile.write('n')
saveFile.close()
return True
except BaseException as e:
print ('failed on data',str(e))
time.sleep(5)

def on_error(self,status):
print (status)

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=['رياضه'],languages=["ar"])


The error appears is:




failed on data 'utf-8' codec can't encode character 'ud83d' in
position 2808: surrogates not allowed and continues to download the
data




how I can solve it can anyone help me.
the program print the text in Arabic but when load it with the next program.



 with io.open('E:\da.json', 'r',encoding ='utf-8') as f: 
line = f.readline()
tweets = json.loads(line)

print(tweets)


The error :




UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe5 in position
111: invalid continuation byte











share|improve this question



























    up vote
    1
    down vote

    favorite












    class listener(StreamListener):
    def on_data(self, data):
    try:
    tweet=data
    t=tweet.encode('utf-8').decode('unicode_escape')
    print(t)
    saveFile = open('E:\da.json','a')
    saveFile.write(t)
    saveFile.write('n')
    saveFile.close()
    return True
    except BaseException as e:
    print ('failed on data',str(e))
    time.sleep(5)

    def on_error(self,status):
    print (status)

    auth = OAuthHandler(ckey, csecret)
    auth.set_access_token(atoken, asecret)
    twitterStream = Stream(auth, listener())
    twitterStream.filter(track=['رياضه'],languages=["ar"])


    The error appears is:




    failed on data 'utf-8' codec can't encode character 'ud83d' in
    position 2808: surrogates not allowed and continues to download the
    data




    how I can solve it can anyone help me.
    the program print the text in Arabic but when load it with the next program.



     with io.open('E:\da.json', 'r',encoding ='utf-8') as f: 
    line = f.readline()
    tweets = json.loads(line)

    print(tweets)


    The error :




    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe5 in position
    111: invalid continuation byte











    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      class listener(StreamListener):
      def on_data(self, data):
      try:
      tweet=data
      t=tweet.encode('utf-8').decode('unicode_escape')
      print(t)
      saveFile = open('E:\da.json','a')
      saveFile.write(t)
      saveFile.write('n')
      saveFile.close()
      return True
      except BaseException as e:
      print ('failed on data',str(e))
      time.sleep(5)

      def on_error(self,status):
      print (status)

      auth = OAuthHandler(ckey, csecret)
      auth.set_access_token(atoken, asecret)
      twitterStream = Stream(auth, listener())
      twitterStream.filter(track=['رياضه'],languages=["ar"])


      The error appears is:




      failed on data 'utf-8' codec can't encode character 'ud83d' in
      position 2808: surrogates not allowed and continues to download the
      data




      how I can solve it can anyone help me.
      the program print the text in Arabic but when load it with the next program.



       with io.open('E:\da.json', 'r',encoding ='utf-8') as f: 
      line = f.readline()
      tweets = json.loads(line)

      print(tweets)


      The error :




      UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe5 in position
      111: invalid continuation byte











      share|improve this question















      class listener(StreamListener):
      def on_data(self, data):
      try:
      tweet=data
      t=tweet.encode('utf-8').decode('unicode_escape')
      print(t)
      saveFile = open('E:\da.json','a')
      saveFile.write(t)
      saveFile.write('n')
      saveFile.close()
      return True
      except BaseException as e:
      print ('failed on data',str(e))
      time.sleep(5)

      def on_error(self,status):
      print (status)

      auth = OAuthHandler(ckey, csecret)
      auth.set_access_token(atoken, asecret)
      twitterStream = Stream(auth, listener())
      twitterStream.filter(track=['رياضه'],languages=["ar"])


      The error appears is:




      failed on data 'utf-8' codec can't encode character 'ud83d' in
      position 2808: surrogates not allowed and continues to download the
      data




      how I can solve it can anyone help me.
      the program print the text in Arabic but when load it with the next program.



       with io.open('E:\da.json', 'r',encoding ='utf-8') as f: 
      line = f.readline()
      tweets = json.loads(line)

      print(tweets)


      The error :




      UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe5 in position
      111: invalid continuation byte








      python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 1 at 11:16









      Banghua Zhao

      1,136617




      1,136617










      asked Nov 8 at 14:11









      esraa

      613




      613



























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209502%2fi-am-trying-to-stream-arabic-tweet-from-an-api-twitter-with-python%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209502%2fi-am-trying-to-stream-arabic-tweet-from-an-api-twitter-with-python%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          What does pagestruct do in Eviews?

          Dutch intervention in Lombok and Karangasem

          Channel Islands