问题描述
我通过python从sqlite获得了结果,就像这样的元组:(u'PR:000017512',)
但是,我想将其打印为'PR:000017512'。首先,我尝试使用索引[0]选择元组中的第一个。但是打印结果仍然是u'PR:000017512'。然后,我使用str()进行转换,但没有任何改变。如何在没有u''的情况下打印此文件?
I got my results from sqlite by python, it's like this kind of tuples: (u'PR:000017512',)However, I wanna print it as 'PR:000017512'. At first, I tried to select the first one in tuple by using index [0]. But the print out results is still u'PR:000017512'. Then I used str() to convert and nothing changed. How can I print this without u''?
推荐答案
您正在将字符串 representation 与它的价值。当您打印Unicode字符串时,不会打印 u
:
You're confusing the string representation with its value. When you print a unicode string the u
doesn't get printed:
>>> foo=u'abc'
>>> foo
u'abc'
>>> print foo
abc
更新:
由于您正在处理元组,所以您很难摆脱这种麻烦:您必须打印元组的成员:
>>> foo=(u'abc',)
>>> print foo
(u'abc',)
>>> # If the tuple really only has one member, you can just subscript it:
>>> print foo[0]
abc
>>> # Join is a more realistic approach when dealing with iterables:
>>> print '\n'.join(foo)
abc
这篇关于python将unicode转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!