找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 490|回复: 1

[求助] python2.7 编码问题

26

主题

61

帖子

61

积分

新手

积分
61
aggopie 发表于 2018-7-10 10:06:00 | 显示全部楼层 |阅读模式
# -*- coding: utf-8 -*-
import sys
reload(sys)  
sys.setdefaultencoding('utf8')

#str = '中文'
#str.encode('gb18030')

class Student(object):
    def __init__(self, name):
        self.name = name

    def __call__(self):
        print('名字是 %s.' % self.name)
s = Student('Michael')



结果还是会出错


debugfile('C:/Users/Administrator/.spyder/temp.py', wdir='C:/Users/Administrator/.spyder')

Traceback (most recent call last):

  File "<ipython-input-12-65e00dbfa397>", line 1, in <module>
    debugfile('C:/Users/Administrator/.spyder/temp.py', wdir='C:/Users/Administrator/.spyder')

  File "D:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 728, in debugfile
    debugger.run("runfile(%r, args=%r, wdir=%r)" % (filename, args, wdir))

  File "D:\ProgramData\Anaconda2\lib\bdb.py", line 400, in run
    exec cmd in globals, locals

  File "<string>", line 1, in <module>

  File "D:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "D:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "c:/users/administrator/.spyder/temp.py", line 2, in <module>
    import sys

  File "c:/users/administrator/.spyder/temp.py", line 2, in <module>
    import sys

  File "D:\ProgramData\Anaconda2\lib\bdb.py", line 49, in trace_dispatch
    return self.dispatch_line(frame)

  File "D:\ProgramData\Anaconda2\lib\bdb.py", line 67, in dispatch_line
    self.user_line(frame)

  File "D:\ProgramData\Anaconda2\lib\pdb.py", line 158, in user_line
    self.interaction(frame, None)

  File "D:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 441, in interaction
    self.print_stack_entry(self.stack[self.curindex])

  File "D:\ProgramData\Anaconda2\lib\site-packages\IPython\core\debugger.py", line 344, in print_stack_entry
    print(self.format_stack_entry(frame_lineno, '', context))

UnicodeEncodeError: 'ascii' codec can't encode characters in position 332-338: ordinal not in range(128)

已经是从网上找说是加入
import sys
reload(sys)  
sys.setdefaultencoding('utf8')
但是还是有问题,真不知道python2.7版本到底怎么搞这个编码问题?
还请大侠们指点

回复

使用道具 举报

26

主题

61

帖子

61

积分

新手

积分
61
aggopie  楼主| 发表于 2018-8-3 16:10:56 | 显示全部楼层
还有人帮忙一下吖
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表