2017-05-06 88 views
0

我该如何处理?UnicodeEncodeError:'cp949'编解码器无法编码字符

wfile.write(数据[ '文本'] + '\ N')

UnicodeEncodeError: 'CP949' 编解码器无法编码的字符

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

我通过修改因特网得到这个错误。

类型错误:写()不带任何关键字参数

wfile.write(data['text']+'\n',encoding='UTF8') 

类型错误:写()接受只有一个参数(2给出)

wfile.write(data['text']+'\n','utf-8') 

回答

2

cp949是默认的语言环境对你的Windows系统,这就是open()的默认值。从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.

指定打开文件时不同的编解码器:

wfile = open(os.getcwd()+"/test1.txt", mode='w', encoding='utf8') 

需要注意的是不带路径打开一个文件时,你不需要预先挂起os.getcwd(),默认为使用工作目录的相对路径:

wfile = open("test1.txt", mode='w', encoding='utf8') 

你会更好使用os.path.join()搭建一切路径。

您的代码可以用enumerate()和上下文管理器进一步简化。 data词典在这里并不是真的有用,只需引用tweet.text无处不在:

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) 
相关问题