Python运行py文件-在IDLE中工作正常,而不是通过命令提示符窗口7-UnicodeEncodeError [英] Python run py file - works OK in IDLE, not via command prompt windows 7 - UnicodeEncodeError

查看:43
本文介绍了Python运行py文件-在IDLE中工作正常,而不是通过命令提示符窗口7-UnicodeEncodeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前,我在Windows 7上安装了3.5.1 Python.我有一个.py文件中的python程序.它可以从IDLE Python正常运行.

I installed 3.5.1 Python several months ago on Windows 7. I have a python program which is in .py file. It runs fine from IDLE Python.

但是,当我从命令提示符运行它时,它说:

However, when I run it from command prompt, it says:

C:\_1\Python>info_.py
START -------------------------
Traceback (most recent call last):
  File "C:\_1\Python\info_.py", line 33, in <module>
    print (data)
  File "C:\Program Files\Python35\lib\encodings\cp866.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xab' in position 18
4: character maps to <undefined>

数据是:

u = urllib.request.urlopen('http://www.****')
data = u.read()

我也想从提示符处运行程序.我该怎么做才能确保我的代码在Windows上的IDLE和通过命令提示符下都可以正常工作?

I want to run programs from prompt too. What could I do to be sure my code works same in IDLE and via command prompt on Windows?

推荐答案

尝试使用编码

print(data.encode('utf-8'))

这篇关于Python运行py文件-在IDLE中工作正常,而不是通过命令提示符窗口7-UnicodeEncodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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