如何将“u”前缀添加到列表或字符串中?

2024-09-30 02:26:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了一些unicode问题,并意识到(无可否认,有点晚了)在字符串中添加“u”前缀可以达到以下目的:

print (u'No\xebl')

Noël

但是,我要处理大量的字符串和字符串列表,因此我需要为每个字符串添加前缀(比如,我想将“u”添加到“string”,其中string='No\xebl')。我尝试过不同的方法:

print "u"+"'"+string

print unicode(string)

print "u" + string

print repr(unicode(m)) #Doing so does add the prefix 'u', but adds an extra "\" to the string and no longer fixes the problem

u'No\xebl'

清单还在继续,但你得到了要点。基本上,我想知道是否有一种方法可以与print(u'No\xebl')完全相同,但是使用任何变量字符串,而不必实际写下字符串。在

如有任何建议,我们将不胜感激!在


Tags: the方法no字符串目的列表stringso
2条回答

\xebISO 8859-1中编码ë。要从字节转换为Unicode字符串,请使用.decode()方法。在

string.decode('iso-8859-1')

话虽如此:这些数据从何而来?你知道它一直是ISO8859-1,还是可能编码不同?为什么它已经是字节而不是Unicode字符串?对这些问题的回答可能会有更好的解决办法。在

这样做

tempvariabel = u''
realvariabel = tempvariabel + somevariabelcontainUnicode

当您想打印它时,它是返回错误,但它已经准备好写入文件或数据库

相关问题 更多 >

    热门问题