Python - 编码字符串 - 瑞典字母 [英] Python - Encoding string - Swedish Letters
问题描述
由于某些原因,raw_input不会得到swedify()产生的转换字符串,这给我一个编码错误,我知道,这就是为什么我开始swedify()开始。
这是我要做的:
elif cmd in('help','hjälp',' info'):
buffert + ='Just nuärprogramme relativtbegränsat,\\\
De funktioner du har attanvändaär:\\\
'
buffert + ='* historik :: skriver ut all din historik \\\
'
buffert + ='*ändra<något> ::ändrarnågoti databasen,följandefinns attändra:\\\
'
print swedify(buffert)
这个工作很好,它输出的瑞典字符就像我想要他们到控制台。
但是当我尝试(在相同的代码中,使用相同的\x ??值,打印此部分:
core ['targetDistance'] = raw_input(swedify('Hurlångti kilometerärdittmål:'))
core ['goalTime'] = raw_input(swedify('Vadärdittmåli minuter att springa' + core ['goalDistance'] +'kmpå:'))
然后我得到这个: / p>
C:\Users\Anon> pythonlöp.py
追溯(最近的最后一次呼叫):
文件l÷p.py,第92行,< module>
core ['goalDistance'] = raw_input(swedify('Hurlångti kilotsärdittmål:') )
UnicodeEncodeError:'ascii'编解码器不能编码位置5中的字符u'\xe5'序号不在范围(128)
现在我已经google了,发现了一些解决方案,但没有一个工作,有些伤心,我必须创建一个批处理脚本,在一开始执行chcp ???但那是不是一个干净的解决方案IMO。
这里是swedify:
def swedify(inp):
try:
return inp.decode('utf-8')
除了:
return'(!Dec :)'+ str(inp)
有关如何获取raw_input从swedify()读取我的返回值的任何解决方案?
我尝试从编码import getencoder,getdecoder和其他,但没有更好的。
解决方案很多问题:
编辑:C:\Python ?? \Lib\Site.py
将del sys.setdefaultencoding替换为pass
然后,
将其放在代码的顶部:
sys.setdefaultencoding('latin-1')
/ pre>
修复瑞典/非UTF8兼容字符的圣杯。
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:
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:
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.
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.
解决方案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:sys.setdefaultencoding('latin-1')
The holy grail of fixing the Swedish/non-UTF8 compatible characters.
这篇关于Python - 编码字符串 - 瑞典字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!