简体   繁体   English

Python 2.6 Unicode打印错误?

[英]Python 2.6 Unicode Print Bug?

Here are three pieces of similar code, the first two run fine, the last one fails -- 这是三段相似的代码,前两段运行良好,最后一段失败-

    # 1
    print '%d) "%s" ' % (new_item_count, item[u'Title'].encode('utf-8')),
    print '%s' % item[u'CurrencyID'],
    print '%s !' % item[u'Value']

    # 2
    print '%d) "%s" %s%s !!' % (new_item_count,
            item[u'Title'].encode('utf-8'),
            item[u'CurrencyID'].encode('utf-8'),
            item[u'Value'])

    # 3
    print '%d) "%s" %s%s !!!' % (new_item_count,
            item[u'Title'].encode('utf-8'),
            item[u'CurrencyID'],
            item[u'Value'])

Output (note the differentiating exclamation marks at the end) -- 输出(请注意最后的区分感叹号)-

37) "HP Pavilion Laptop / Intel® Core™ i3 Processor"  USD 510.0 !
37) "HP Pavilion Laptop / Intel® Core™ i3 Processor" USD510.0 !!
'ascii' codec can't decode byte 0xc2 in position 29: ordinal not in range(128)

This is under Python 2.6.5 (Ubuntu 10.04 package, same behaviour in both 32bit and 64bit) -- 这是在Python 2.6.5(Ubuntu 10.04软件包,在32位和64位上具有相同的行为)下的-

$ python -V
Python 2.6.5
$ uname -a
Linux <> 2.6.39.1-x86_64-<> #1 SMP Tue Jun 21 10:04:20 EDT 2011 x86_64 GNU/Linux

The only explanation I can think of is that it's a Python bug. 我能想到的唯一解释是这是一个Python错误。

Or is it? 还是?

You might even have triggered some incosisntency in there, but as it is, your code - in all of the examples. 在所有示例中,您甚至可能在其中触发了一些无关紧要的内容,但实际上是您的代码。 is somewhat messy. 有点混乱。

You should not mix unicode and non-unicode strings like that 您不应该这样混合unicode和非unicode字符串

Please, tale a look at http://www.joelonsoftware.com/articles/Unicode.html - the article gives one a nice understanding of what unicode and different encodings actually are, and make it a lot easier to code anything afterwards. 请让我们看一下http://www.joelonsoftware.com/articles/Unicode.html -本文对unicode和不同的编码实际上有一个很好的了解,并且使以后编写任何代码变得容易得多。

As for your specific snippet, if all your data in the "item" dictionary is appropriately in unicode, as it seens, just do the string interpolation in unicode, and just encode the final resulting string to the intended output format: 至于您的特定代码段,如您所见,如果“ item”字典中的所有数据都适当地采用了unicode,只需用unicode进行字符串插值,然后将最终的最终字符串编码为预期的输出格式即可:

message = u'%d) "%s" %s%s !!!' % (new_item_count,
            item[u'Title'],
            item[u'CurrencyID'],
            item[u'Value']))
print message.encode("utf-8")

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

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