当前位置:   article > 正文

python unicode 转码,python将unicode转换为字符串

python unicode转字符串

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''?

解决方案

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

Update:

Since you're dealing with a tuple, you don't get off this easy: You have to print the members of the tuple:

>>> 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/153724?site
推荐阅读
相关标签
  

闽ICP备14008679号