在 Python 中解码和编码希伯来语字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29850912/
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
decoding and encoding Hebrew string in Python
提问by user1767774
I am trying to encode and decode the Hebrew string "????". However, after encoding, I get gibberish:
我正在尝试对希伯来语字符串“????”进行编码和解码。但是,编码后,我得到胡言乱语:
>>> word = "????"
>>> word = word.decode('UTF-8')
>>> word
u'\u05e9\u05dc\u05d5\u05dd'
>>> print word
????
>>> word = word.encode('UTF-8')
>>> word
'\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d'
>>> print word
??????
How should I do it propely?
我应该怎么做?
Thanks.
谢谢。
采纳答案by jonhurlock
You'll have to make sure you have the right encoding in your environment (shell or script). If you're using a script include the following:
您必须确保在您的环境(shell 或脚本)中有正确的编码。如果您使用的是脚本,请包括以下内容:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
To make sure your environment knows you're using UTF-8. You may find that your shell terminal will accept only ASCII, so make sure it is able to support UTF-8.
确保您的环境知道您使用的是 UTF-8。您可能会发现您的 shell 终端只接受 ASCII,因此请确保它能够支持 UTF-8。
>>> word = "????"
>>> word
'\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d'
>>> print word
????
>>> word = word.decode('UTF-8')
>>> word
u'\u05e9\u05dc\u05d5\u05dd'
>>> print word
????
>>> word = word.encode('UTF-8')
>>> word
'\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d'
>>> print word
????
>>>