简体   繁体   English

Python 2.X中的拆分和非拉丁字符串

[英]Split and non-latin strings in Python 2.X

Example: 例:

# -*- coding: utf-8 -*-
my_str = u'Строка ^ с ^ разделителями!' # Russian letters
print my_str.replace(' ', '')
print my_str.replace(' ', '').split('^')

Result: 结果:

Строка^с^разделителями!
[u'\u0421\u0442\u0440\u043e\u043a\u0430 ', u' \u0441 ', u' \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044f\u043c\u0438!']

Please, help. 请帮忙。 How can I show 'normal' strings after splitting? 分裂后如何显示“正常”字符串?

PS File-script encoding is utf8 PS文件脚本编码是utf8

These are normal strings, you're just seeing their internal representation (because you're not printing a string, you're printing a list in the second example). 这些普通的字符串,你只是看到它们的内部表示(因为你没有打印字符串,你在第二个例子中打印一个列表)。 Do

for s in my_str.replace(' ', '').split('^'):
    print s

and you'll see. 你会看到的。 Conversely, try 相反,试试吧

print repr(my_str.replace(' ', ''))

and see what happens then. 然后看看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM