result = ['袁惟仁', None, 'Life']
# Replace None with empty string
response = ['' if s is None else s for s in result]
# Handle non-ascii characters
return [s.encode('utf-8') if isinstance(s, unicode) else str(s) for s in response]
在这段代码中,我用“”替换list的None值,然后处理Unicode字符。这很管用,但我想知道是否有更好的方法。现在如果我不处理empty,我会得到一个错误,说不能将None转换为str
filter(None, result)
将从列表中删除任何None
:使用
or
和列表理解表达式可以得到相同的结果:顺便说一句,我不明白你为什么要显式地做
.encode('utf-8')
。你知道吗相关问题 更多 >
编程相关推荐