How do I handle this?
wfile.write(data[\'text\']+\'\\n\')
UnicodeEncodeError: \'cp949\' codec can\'t encode character
import tweepy
import time
cp949
is the default locale for your Windows system, and that's what open()
defaults to. From the open() documentation:
encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent (whatever
locale.getpreferredencoding()
returns), but any text encoding supported by Python can be used.
Specify a different codec when opening the file:
wfile = open(os.getcwd()+"/test1.txt", mode='w', encoding='utf8')
Note that you don't need to pre-pend os.getcwd()
when opening a file without a path, the default is to use the working directory for relative paths:
wfile = open("test1.txt", mode='w', encoding='utf8')
You'd be better off using os.path.join()
to build paths for everything else.
Your code can otherwise be simplified further with enumerate()
and a context manager. The data
dictionary is not really useful here, just reference tweet.text
everywhere:
with open(os.getcwd()+"/test1.txt", mode='w') as wfile:
for i, tweet in enumerate(c.items()):
print("{}: {}".format(i, tweet.text))
wfile.write(tweet.text + '\n')
time.sleep(0.5)