UnicodeEncodeError: 'cp949' codec can'

2019-08-07 16:52发布

问题:

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')  

回答1:

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)