带有斯堪的纳维亚字母的 Python 列表 [英] Python lists with scandinavic letters

查看:25
本文介绍了带有斯堪的纳维亚字母的 Python 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下是什么原因造成的,以便更好地了解环境?

Can anyone explain what causes this for better understanding of the environment?

emacs、unix

emacs, unix

输入:

with open("example.txt", "r") as f:
    for files in f:
        print files
        split = files.split()
        print split

输出:

Hello world
['Hello', 'world']
Hello wörld
['Hello', 'wxf6rld']

推荐答案

Python 正在打印字符串表示,其中包括不可打印的字节.不可打印的字节(ASCII 范围之外的任何内容或控制字符)显示为转义序列.

Python is printing the string representation, which includes a non-printable byte. Non-printable bytes (anything outside the ASCII range or a control character) is displayed as an escape sequence.

关键是您可以复制该表示并将其粘贴到 Python 代码或解释器中,从而产生完全相同的值.

The point is that you can copy that representation and paste it into Python code or into the interpreter, producing the exact same value.

xf6 转义码表示一个具有十六进制值 F6 的字节,当它被解释为拉丁 1 字节值时,就是 ö 字符.

The xf6 escape code represents a byte with hex value F6, which when interpreted as a Latin-1 byte value, is the ö character.

您可能希望将该值解码为 Unicode 以一致地处理数据.如果您还不知道 Unicode 到底是什么,或者想了解有关编码的任何其他信息,请参阅:

You probably want to decode that value to Unicode to handle the data consistently. If you don't yet know what Unicode really is, or want to know anything else about encodings, see:

  • The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky

Python Unicode HOWTO

Pragmatic Unicode 作者 Ned Batchelder

Pragmatic Unicode by Ned Batchelder

这篇关于带有斯堪的纳维亚字母的 Python 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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