windows Python - 编码字符串 - 瑞典字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7315629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - Encoding string - Swedish Letters
提问by Torxed
I'm having some trouble with Python's raw_input command (Python2.6), For some reason, the raw_input does not get the converted string that swedify() produces and this giving me a encoding error which i'm aware of, that's why i made swedify() to begin with. Here's what i'm trying to do:
我在使用 Python 的 raw_input 命令(Python2.6)时遇到了一些问题,出于某种原因,raw_input 没有得到 swedify() 生成的转换后的字符串,这给了我一个我知道的编码错误,这就是为什么我使 swedify() 开始。这是我想要做的:
elif cmd in ('help', 'hj?lp', 'info'):
buffert += 'Just nu ?r programmet relativt begr?nsat,\nDe funktioner du har att anv?nda ?r:\n'
buffert += ' * historik :: skriver ut all din historik\n'
buffert += ' * ?ndra <n?got> :: ?ndrar n?got i databasen, f?ljande finns att ?ndra:\n'
print swedify(buffert)
This works just fine, it outputs the swedish characters just as i want them to the console. But when i try to (in the same code, with same \x?? values, print this piece:
这工作得很好,它输出瑞典字符,就像我希望它们到控制台一样。但是当我尝试(在相同的代码中,使用相同的 \x?? 值时,打印这一段:
core['goalDistance'] = raw_input(swedify('Hur l?ngt i kilometer ?r ditt m?l: '))
core['goalTime'] = raw_input(swedify('Vad ?r ditt m?l i minuter att springa ' + core['goalDistance'] + 'km p?: '))
Then i get this:
然后我得到这个:
C:\Users\Anon>python l?p.py
Traceback (most recent call last):
File "l÷p.py", line 92, in <module>
core['goalDistance'] = raw_input(swedify('Hur l├?ngt i kilometer ├?r ditt m├?l: '))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 5: ordinal not in range(128)
Now i've googled around, found some "solutions" but none of them work, some sad that i have to create a batch script that executes chcp ??? in the beginning, but that's not a clean solution IMO.
现在我四处搜索,找到了一些“解决方案”,但它们都不起作用,有些遗憾的是我必须创建一个执行 chcp 的批处理脚本???一开始,但这不是一个干净的解决方案 IMO。
Here is swedify:
这是瑞典:
def swedify(inp):
try:
return inp.decode('utf-8')
except:
return '(!Dec:) ' + str(inp)
Any solutions on how to get raw_input to read my return value from swedify()? i've tried from encodings import getencoder, getdecoder and others but nothing for the better.
关于如何让 raw_input 从 swedify() 读取我的返回值的任何解决方案?我已经尝试过从编码导入 getencoder、getdecoder 和其他,但没有更好的。
采纳答案by Torxed
Solution to a lot of problems:
Edit: C:\Python??\Lib\Site.py
Replace "del sys.setdefaultencoding" with "pass"
Then,
Put this in the top of your code:
很多问题的解决方法:
编辑:C:\Python??\Lib\Site.py 将“del sys.setdefaultencoding”替换为“pass”
然后,
把这个放在你代码的顶部:
sys.setdefaultencoding('latin-1')
The holy grail of fixing the Swedish/non-UTF8 compatible characters.
修复瑞典语/非 UTF8 兼容字符的圣杯。
回答by Ray Toal
You mention the fact that you received an encoding error which motivated you to write swedify
in the first place, and you have found solutions around chcp
which is a Windows command.
您提到了这样一个事实,即您收到了一个编码错误,这促使您首先编写代码swedify
,并且您已经找到了解决方案,chcp
这是一个 Windows 命令。
On *nix systems with UTF-8 terminals, swedify
is not necessary:
在带有 UTF-8 终端的 *nix 系统上,swedify
不需要:
>>> raw_input('Hur l?ngt i kilometer ?r ditt m?l: ')
Hur l?ngt i kilometer ?r ditt m?l: 100
'100'
>>> a = raw_input('Hur l?ngt i kilometer ?r ditt m?l: ')
Hur l?ngt i kilometer ?r ditt m?l: 200
>>> a
'200'
FWIW, when I douse swedify
, I get the same error you do:
FWIW,当我做用swedify
,我让你做同样的错误:
>>> def swedify(inp):
... try:
... return inp.decode('utf-8')
... except:
... return '(!Dec:) ' + str(inp)
...
>>> swedify('Hur l?ngt i kilometer ?r ditt m?l: ')
u'Hur l\xe5ngt i kilometer \xe4r ditt m\xe5l: '
>>> raw_input(swedify('Hur l?ngt i kilometer ?r ditt m?l: '))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 5: ordinal not in range(128)
Your swedify
function returns a unicode object. The built-in raw_input
is just not happy with unicode objects.
您的swedify
函数返回一个 unicode 对象。内置raw_input
只是对 unicode 对象不满意。
>>> raw_input("?")
?eee
'eee'
>>> raw_input(u"?")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 0: ordinal not in range(128)
You might want to try this in Python 3. See this Python bug.
您可能想在 Python 3 中尝试此操作。请参阅此Python 错误。
Also of interest: How to read Unicode input and compare Unicode strings in Python?.
同样感兴趣:如何在 Python 中读取 Unicode 输入并比较 Unicode 字符串?.
UPDATEAccording to this blog postthere is a way to set the system's default encoding. This might be worth a try.
更新根据这篇博客文章,有一种方法可以设置系统的默认编码。这可能值得一试。
回答by Per Persson
For me it worked fine with:
对我来说,它适用于:
#-*- coding: utf-8 -*-
import sys
import codecs
koden=sys.stdin.encoding
a=raw_input( u'Fr?gan ?r ?ppen? '.encode(koden))
print a
Per
每
回答by Alastair McCormack
On Windows, the console's native Unicode support is broken. Even the apparent UTF-8 codepage isn't a proper fix.
在 Windows 上,控制台的本机 Unicode 支持已损坏。即使是明显的 UTF-8 代码页也不是正确的修复。
To read and write with Windows console you need use https://github.com/Drekin/win-unicode-console, which works directly with the underlying console API, so that multi-byte characters are read and written correctly.
要使用 Windows 控制台读取和写入,您需要使用https://github.com/Drekin/win-unicode-console,它直接与底层控制台 API 配合使用,以便正确读取和写入多字节字符。
回答by Tim Gremalm
Windows command prompt uses Codepage 850 when using Swedish regional settings (https://en.wikipedia.org/wiki/Code_page_850). It's probably used because of backwards compatibility with old MS-Dos programs.
Windows 命令提示符在使用瑞典区域设置 ( https://en.wikipedia.org/wiki/Code_page_850)时使用代码页 850 。使用它可能是因为与旧的 MS-Dos 程序向后兼容。
You can set Windows command prompt to use UTF-8 as encoding by entering:
chcp 65001
(Unicode characters in Windows command line - how?)
您可以通过输入以下内容将 Windows 命令提示符设置为使用 UTF-8 作为编码:
chcp 65001
(Windows 命令行中的 Unicode 字符 - 如何?)
回答by Fabian
Try this magic comment at the very top of your script:
在脚本的最顶部试试这个神奇的注释:
# -*- coding: utf-8 -*-
Here is some information about it: http://www.python.org/dev/peps/pep-0263/
以下是有关它的一些信息:http: //www.python.org/dev/peps/pep-0263/