2017-05-06 89 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) 
相關問題