UnicodeEncodeError:'cp949'编解码器无法编码字符 [英] UnicodeEncodeError: 'cp949' codec can't encode character

查看:402
本文介绍了UnicodeEncodeError:'cp949'编解码器无法编码字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何处理?

wfile.write(data ['text'] +'\ n')

wfile.write(data['text']+'\n')

UnicodeEncodeError:'cp949'编解码器无法编码字符

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

我通过修改Internet收到此错误.

I get this error by modifying the Internet.

TypeError:write()不使用关键字参数

TypeError: write() takes no keyword arguments

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

TypeError:write()仅接受一个参数(给定2个参数)

TypeError: write() takes exactly one argument (2 given)

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

推荐答案

cp949是Windows系统的默认语言环境,而这正是open()的默认语言环境.从 open()文档:

cp949 is the default locale for your Windows system, and that's what open() defaults to. From the open() documentation:

encoding 是用于解码或编码文件的编码名称.仅应在文本模式下使用.默认编码取决于平台(无论locale.getpreferredencoding()返回什么),但是可以使用Python支持的任何文本编码.

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

请注意,当打开没有路径的文件时,您无需在前面加上os.getcwd(),默认是使用工作目录作为相对路径:

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

最好使用os.path.join()为其他所有路径构建路径.

You'd be better off using os.path.join() to build paths for everything else.

否则,您可以使用enumerate()和上下文管理器进一步简化您的代码. data字典在这里并不是真正有用的,只是在任何地方都引用tweet.text:

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)

这篇关于UnicodeEncodeError:'cp949'编解码器无法编码字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆