简体   繁体   English

如何使用raw_input获取中文字符

[英]How can I get chinese character using raw_input

my dev enviroment is: eclipse+pydev. 我的开发环境是:eclipse + pydev。

If I use raw_input() to get character, I input "你好世界", then I get "浣犲ソ涓栫晫". 如果我使用raw_input()获取角色,我输入“你好世界”,然后我得到“浣ソ涓涓栫晫”。 Then how can I get "你好世界" and print it correctly. 那我怎么才能得到“你好世界”并正确打印出来。

I have tried raw_input().decode(sys.stdin.encoding), but the result is same. 我尝试过raw_input()。decode(sys.stdin.encoding),但结果是一样的。

Decode using the terminal's/console's code page. 使用终端/控制台的代码页解码。

import sys

t = raw_input().decode(sys.stdin.encoding)
print t

Check the encoding you are using. 检查您正在使用的编码。 Based on @imom0's comment , I went and tried gbk encoding. 根据@ imom0的评论 ,我去尝试了gbk编码。 Specifically, this is my python 2.7.3 interpreter with UTF-8 encoding via ibus for input: 具体来说,这是我的python 2.7.3解释器,通过ibus进行UTF-8编码输入:

>>> print raw_input().decode('gbk')
你好世界
浣犲ソ涓栫晫
>>> print raw_input().decode('utf-8')
你好世界
你好世界

This is the result of trying to decode a UTF-8 encoded string as gbk . 这是尝试将UTF-8编码的字符串解码为gbk Since your input seems to be some form of UTF, why not enforce either utf-8 decoding or use the input's encoding to decode it, as in @ignacio-vazquez-abrams' answer ? 由于您的输入似乎是某种形式的UTF,为什么不强制执行utf-8解码或使用输入的编码来解码它,如@ ignacio-vazquez-abrams的回答

import sys

print myString.decode(sys.stdin.encoding)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM