【问题标题】:How to concatenate multiple unicode string?如何连接多个unicode字符串?
【发布时间】:2012-11-05 13:48:19
【问题描述】:

我有两个 unicode 字符串 '가''ㄱ',我想将它们连接起来得到 "가ㄱ"

这是我的代码:

output1 = unicodeQueue(self.queue) # first unicode result
output2 = unicodeQueue(self.bufferQueue) # second unicode result
sequence = [output1, output2]
print sequence
output = ''.join(sequence)
return output

这是我得到的输出:

[u'\uac00', u'\u3131']
ㄱ가가ㄱ가

我不知道为什么它没有产生正确的结果,谁能帮我解决这个问题?

【问题讨论】:

  • 我无法重现此内容。
  • @NPE 我上传了我的部分脚本,我的主要问题是“如何在 python 中连接两个 unicode?”
  • @user1732445:您的代码没有明显错误。
  • 为我工作。可以试试u''.join(sequence)
  • @KennyTM 嗯。谢谢..现在它对我有用...

标签: python unicode python-2.x


【解决方案1】:

如果你想连接两个字符串,请使用+

>>> '가' + 'ㄱ'
'\xea\xb0\x80\xe3\x84\xb1'
>>> u'가' + u'ㄱ'
u'\uac00\u3131'
>>> print u'가' + u'ㄱ'
가ㄱ

这意味着你可以使用

output1 + output2

【讨论】:

  • 请记住,+ 的工作速度比 unicode.join(u'',vals)str.join('',vals) 慢,但如果它在 join 不起作用时工作,一定要使用它。
猜你喜欢
  • 1970-01-01
  • 2012-12-04
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
  • 2023-03-03
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
相关资源
最近更新 更多