Unicode,命令行和空闲 [英] Unicode, command-line and idle

查看:75
本文介绍了Unicode,命令行和空闲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,以下程序提示用户输入一个单词并测试

查看它是否与另一个相同。如果用户键入espa?a, (注意

该单词包含''?''),程序应输出相同。如果我在IDLE中运行代码,那么

是有效的,但如果我在windows

控制台中运行它则不行。有人可以解释一下为什么会发生这种情况以及如何解决这个问题吗?

# - * - 编码:cp1252 - * -

text1 =''espa?a''

text2 = raw_input()

如果text1 == text2:

print''相同''

否则:

打印''不一样''

解决方案

< blockquote> a.*******@vielca.com 写道:

您好,以下程序提示用户输入一个单词并进行测试以确定它是否与另一个单词相同。如果用户键入espa?a, (注意
该单词包含''?''),程序应输出相同。如果我在IDLE中运行代码但是如果我在windows
控制台中运行代码则不行。有人可以解释一下为什么会发生这种情况以及如何解决这个问题吗?

# - * - 编码:cp1252 - * -
text1 =''espa?a''
text2 = raw_input()
如果text1 == text2:
打印''相同''
否则:
打印''不一样''




我认为raw_input根本不会解码非ascii输入。和你的控制台

可能使用cp850而不是cp1252。在

中进行比较unicode空间应该更好用:


import sys

text1 = u''espa?a' '

text2 = unicode(raw_input(),sys.stdin.encoding)

如果text1 == text2:

打印''相同' '

否则:

打印''不一样''


< / F>


(仅用于确认)

嗨!

如果控制台在cp1252中,raw_input工作正常,用? ;


这个(你的)剧本:

# - * - 编码:cp1252 - * -


导入sys

text1 = u''espa?a''

text2 = unicode(raw_input(),sys.stdin.encoding)

if text1 == text2:

打印''相同''

否则:

打印''不一样''

与chcp 850一起正常工作和chcp 1252


@ -salutations


Michel Claveau

Fredrik Lundh写道:

a.*******@vielca。 com 写道:

您好,以下程序会提示用户输入一个单词并进行测试以确定它是否与另一个单词相同。如果用户键入espa?a, (注意
该单词包含''?''),程序应输出相同。如果我在IDLE中运行代码但是如果我在windows
控制台中运行代码则不行。有人可以解释一下为什么会发生这种情况以及如何解决这个问题吗?

# - * - 编码:cp1252 - * -
text1 =''espa?a''
text2 = raw_input()
如果text1 == text2:
打印''相同''
否则:
打印''不一样''



我认为raw_input根本不会解码非ascii输入。和你的控制台
可能使用cp850而不是cp1252。在unicode空间中进行比较应该更好:

import sys
text1 = u''espa?a''
text2 = unicode(raw_input() ,sys.stdin.encoding)
如果text1 == text2:
打印''相同''
否则:
打印''不一样''

< / F>




如果我使用控制台这会有效但如果我使用则会出现以下错误

IDLE:


回溯(最近一次调用最后一次):

文件C:\ test.py,第4行,在?

text2 = unicode(raw_input(),sys.stdin.encoding)

AttributeError:PyShell实例没有属性''encoding''

Hello, the following program prompts the user for a word and tests to
see if it is the same as another one. If the user types "espa?a" (note
that the word contains an ''?''), the program should output "same". This
works if I run the code in IDLE but does not if I run it in the windows
console. Can someone explain me why this happens and how to get around
it?

# -*- coding: cp1252 -*-
text1 = ''espa?a''
text2 = raw_input()
if text1 == text2:
print ''same''
else:
print ''not same''

解决方案

a.*******@vielca.com wrote:

Hello, the following program prompts the user for a word and tests to
see if it is the same as another one. If the user types "espa?a" (note
that the word contains an ''?''), the program should output "same". This
works if I run the code in IDLE but does not if I run it in the windows
console. Can someone explain me why this happens and how to get around
it?

# -*- coding: cp1252 -*-
text1 = ''espa?a''
text2 = raw_input()
if text1 == text2:
print ''same''
else:
print ''not same''



I don''t think raw_input decodes non-ascii input at all. and your console
probably uses cp850 rather than cp1252. doing the comparision over in
unicode space should work better:

import sys
text1 = u''espa?a''
text2 = unicode(raw_input(), sys.stdin.encoding)
if text1 == text2:
print ''same''
else:
print ''not same''

</F>


(just for confirm)
Hi!
if the console is in cp1252, raw_input work OK with "?"

This (your) script :
# -*- coding: cp1252 -*-

import sys
text1 = u''espa?a''
text2 = unicode(raw_input(), sys.stdin.encoding)
if text1 == text2:
print ''same''
else:
print ''not same''
work OK with "chcp 850" AND "chcp 1252"

@-salutations

Michel Claveau


Fredrik Lundh wrote:

a.*******@vielca.com wrote:

Hello, the following program prompts the user for a word and tests to
see if it is the same as another one. If the user types "espa?a" (note
that the word contains an ''?''), the program should output "same". This
works if I run the code in IDLE but does not if I run it in the windows
console. Can someone explain me why this happens and how to get around
it?

# -*- coding: cp1252 -*-
text1 = ''espa?a''
text2 = raw_input()
if text1 == text2:
print ''same''
else:
print ''not same''



I don''t think raw_input decodes non-ascii input at all. and your console
probably uses cp850 rather than cp1252. doing the comparision over in
unicode space should work better:

import sys
text1 = u''espa?a''
text2 = unicode(raw_input(), sys.stdin.encoding)
if text1 == text2:
print ''same''
else:
print ''not same''

</F>



This works if I use the console but gives the following error if I use
IDLE:

Traceback (most recent call last):
File "C:\test.py", line 4, in ?
text2 = unicode(raw_input(), sys.stdin.encoding)
AttributeError: PyShell instance has no attribute ''encoding''


这篇关于Unicode,命令行和空闲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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