找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2523|回复: 2

[求助] 新人求助:字符串输出乱码的问题

3

主题

4

帖子

4

积分

贫民

积分
4
lw230yt 发表于 2017-12-25 20:46:44 | 显示全部楼层 |阅读模式


try:

    data=open(r'G:\\Python36\\HeadFirstPython\\Chapter3\\sketch.txt','r')

    for each_line in data:

        try:
            (role, line_spoken) = each_line.split(':', 1)
            print(role, end='')
            print(' said: ', end='')
            print(line_spoken, end='')
        except ValueError:
            pass

    data.close()

except IOError:
    print('The data file is missing!')

输出时第一个字母为乱码
锘縈an said:  Is this the right room for an argument?
Other Man said:  I've told you once.
Man said:  No you haven't!
Other Man said:  Yes I have.
Man said:  When?
Other Man said:  Just now.
Man said:  No you didn't!
Other Man said:  Yes I did!
Man said:  You didn't!
Other Man said:  I'm telling you, I did!
Man said:  You did not!
Other Man said:  Oh I'm sorry, is this a five minute argument, or the full half hour?
Man said:  Ah! (taking out his wallet and paying) Just the five minutes.
Other Man said:  Just the five minutes. Thank you.
Other Man said:  Anyway, I did.
Man said:  You most certainly did not!
Other Man said:  Now let's get one thing quite clear: I most definitely told you!

Windows下python3.6.4版本IDLE编写的代码
求高人指点
谢谢!!!

回复

使用道具 举报

7

主题

33

帖子

33

积分

贫民

积分
33
bigpang 发表于 2017-12-26 11:33:33 | 显示全部楼层
windows 下python 3.6.4 IDLE编写的代码,也要看运行的窗口。cmd窗口是需要编码gbk的。当然,还要看你读取的文件本身的保存时的编码。
你试试把文件保存时用utf-8,然后在ide里面运行,看什么情况。
回复 支持 反对

使用道具 举报

3

主题

4

帖子

4

积分

贫民

积分
4
lw230yt  楼主| 发表于 2017-12-26 17:07:35 | 显示全部楼层
谢谢,把文件保存为utf-8编码,就没有问题了
回复 支持 反对

使用道具 举报

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

本版积分规则

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