如何让python附加var而不更改它?
我假设您希望将unicode放入列表中:如何让python附加var而不更改它?,python,python-2.7,Python,Python 2.7,我假设您希望将unicode放入列表中: import twitter import unicodedata import string def get_tweets(user): resultado=[] temp=[] api=twitter.Api()# statuses=api.GetUserTimeline(user) for tweet in statuses: var = unicodedata.normalize
import twitter
import unicodedata
import string
def get_tweets(user):
resultado=[]
temp=[]
api=twitter.Api()#
statuses=api.GetUserTimeline(user)
for tweet in statuses:
var = unicodedata.normalize('NFKD', tweet.text).encode('utf-8', 'replace')
print var# Horóscopo when i dont append it
resultado.append(var)
print resultado# Horo\xcc\x81scopo, mie\xcc\x81rcoles i get these when i append them
get_tweets('HoroscopoDeHoy')
我认为问题在于print命令。Print在列表上运行字符串转换,在将任何“有趣”字符写入标准输出之前,将其转义。如果要在同一行上显示每个项目,我建议您执行以下操作:
var = unicodedata.normalize('NFKD', tweet.text)
resultado.append( var )
temp.append(var.encode('utf-8', 'ignore'))
这将绕过列表上的字符串转换
资料来源:
我无法在我的机器上运行,所以。。。我不知道你在看什么,Python 2还是Python 3?字符串/unicode在这两者之间是不同的。您是否可以将行
print resultado[0]
?添加到python 2.7中。在执行这两行之后:var=unicodedata.normalize('NFKD',tweet.text)var.encode('utf-8','replace'))我得到了这些输出Horóscopo和带重音的单词,我想在结果中附加带重音的单词,但当我附加它们时,我得到了这个Horo\xcc\x81scopo,我怎么能用重音附加它呢?从上面抓起注释。它是python 2.7。执行这两行之后:var=unicodedata.normalize('NFKD',tweet.text.)、encode('utf-8','replace')打印var,我得到这些输出Horóscopo一个带重音的单词,我想在结果中附加带重音的单词。但是当我执行另外两条语句resultado.append(var)print resultado时,我得到了这个Horo\xcc\x81scopo,我怎么能用重音符号附加它呢?Marco我的问题是,当我将它转换为utf-8时,我希望在附加它时保持这样。我认为这只是显示的问题。两种情况下的值相同。但当打印结果时,它会显示在列表中。如果执行打印结果到[0],会发生什么情况?我希望它返回带有重音符号的整个结果列表,并且只打印第一个结果作为旁注,列表中存储的值仍然应该是unicode字符串。您看到的唯一变化是在显示时发生的。(正如Marco de Wit在上文中所说)
for item in resultado:
print item,