繁体   English   中英

Python 2.6 Unicode打印错误?

[英]Python 2.6 Unicode Print Bug?

这是三段相似的代码,前两段运行良好,最后一段失败-

    # 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'])

输出(请注意最后的区分感叹号)-

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)

这是在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

我能想到的唯一解释是这是一个Python错误。

还是?

在所有示例中,您甚至可能在其中触发了一些无关紧要的内容,但实际上是您的代码。 有点混乱。

您不应该这样混合unicode和非unicode字符串

请让我们看一下http://www.joelonsoftware.com/articles/Unicode.html -本文对unicode和不同的编码实际上有一个很好的了解,并且使以后编写任何代码变得容易得多。

至于您的特定代码段,如您所见,如果“ 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