How do I handle this?
wfile.write(data['text']+'\n')
UnicodeEncodeError: 'cp949' codec can't encode character
import tweepy
import time
import os
import json
search_term1 = ''
search_term2 = ''
lat = ""
lon = ""
radius = ""
location = "%s,%s,%s" % (lat, lon, radius)
auth = tweepy.OAuthHandler(API_key, API_secret)
auth.set_access_token(Access_token, Access_token_secret)
api = tweepy.API(auth)
c=tweepy.Cursor(api.search,
q="{}+OR+{}".format(search_term1, search_term2),
rpp=1000,
geocode=location,
include_entities=True)
wfile = open(os.getcwd()+"/test1.txt", mode='w')
data = {}
i = 1
for tweet in c.items():
data['text'] = tweet.text
print(i, ":", data)
wfile.write(data['text']+'\n')
time.sleep(0.5)
i += 1
wfile.close()
I get this error by modifying the Internet.
TypeError: write() takes no keyword arguments
wfile.write(data['text']+'\n',encoding='UTF8')
TypeError: write() takes exactly one argument (2 given)
wfile.write(data['text']+'\n','utf-8')
cp949
is the default locale for your Windows system, and that's whatopen()
defaults to. From theopen()
documentation:Specify a different codec when opening the file:
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: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. Thedata
dictionary is not really useful here, just referencetweet.text
everywhere: