0

I am writing a tweepy based bot. I have the desired information being pulled and am trying to write that information to a text file for further parsing. However I am having a problem with f.write and am unsure how to approach the issue. Forgive me if this is covered elsewhere, while I searched I'm still pretty new to programming.

I left out authentication.

#!/usr/bin/python

searchQuery = 'foo'
fName = 'log.txt'
tweets = api.search(q=searchQuery,count=3,result_type="recent")
with open(fName, 'w') as f:
    for tweet in tweets:
        tweetText = tweet.text
        f.write(tweetText)
        print [tweetText]

error: UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 139: ordinal not in range(128)

ImNotLeet
  • 381
  • 5
  • 19
  • 3
    try `open(fName, 'w', encoding='utf-8')` or `codecs.open`; see http://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20 – J.J. Hakala Jan 28 '16 at 19:30
  • are you using python 2.x or 3.x in python 3.x your print statement needs a parenthesis. – mugabits Jan 28 '16 at 19:31
  • 2.x sorry for leaving that out. – ImNotLeet Jan 28 '16 at 19:34
  • @J.J.Hakala when I attempt to use encoding the following error populates. with 'open(fName,'w',encoding='utf-8') as f: TypeError: 'encoding' is an invalid keyword argument for this function' I read your link, I don't think I grasp it. I appreciate the help. – ImNotLeet Jan 28 '16 at 19:42
  • 2
    @ImNotLeet try `import codecs` and `codecs.open(fName, 'w', encoding='utf-8')` instead, since you are using python 2.x – J.J. Hakala Jan 28 '16 at 19:43
  • 1
    @J.J.Hakala Ty sir that resolved my issue. – ImNotLeet Jan 28 '16 at 19:46

1 Answers1

1

You have to verify what you're receiving from your call. Python 2.x only prints if it can be encoded in ASCII. Also, the unicode (U+2026) represents an horizontal ellipsis, which tells that perhaps the text is being truncated.

Try

 print tweetText.encode("utf-8")
mugabits
  • 1,015
  • 1
  • 12
  • 22