如果unicode仅为ascii格式,是否可以使用str()将unicode转换为python中的字符串?

如果unicode仅为ascii格式,是否可以使用str()将unicode转换为python中的字符串?,python,pygame,Python,Pygame,我使用的是pygame.KEYDOWN属性unicode,但我需要将其转换为普通字符串。如果我知道str()是简单的ascii码,我可以用它吗?是的,你可以这样做。例如: unicodetext=u"hello there" print(type(unicodetext)) asciitext = str(unicodetext) print(asciitext) print(type(asciitext)) 产生 <type 'unicode'> hello there <

我使用的是pygame.KEYDOWN属性unicode,但我需要将其转换为普通字符串。如果我知道str()是简单的ascii码,我可以用它吗?

是的,你可以这样做。例如:

unicodetext=u"hello there"
print(type(unicodetext))
asciitext = str(unicodetext)
print(asciitext)
print(type(asciitext))
产生

<type 'unicode'>
hello there
<type 'str'>

你好

我觉得这是你刚刚做的事情之一。。。。试试看,你为什么要这么做?你的目标是什么?另外,您使用哪种Python版本?