访问乌尔都语脚本的字符
accessing characters of urdu script
我有以下字符串
test="ن گ ب ن د ی ک ر و ا ن "
我想要的是我想访问每个字符并将其保存在一些变量中以供将来访问但是当我遍历它们时我变得很奇怪output.Actually我不太了解编码方案。
for i in test:
print(i)
上面的代码给了我一些奇怪的字符我想要的是原始脚本字符吗?
对于 Python 2.x 试试这个:
test=u"ن گ ب ن د ی ک ر و ا ن "
for i in test:
print(i)
追加 u
使其成为 unicode
对象。
要么将 test
定义为 unicode 字符串,要么使用 decode
方法:
test="ن گ ب ن د ی ک ر و ا ن"
for i in test.decode('utf8'):
print(i)
# print unicode value
print(repr(i))
test=u"ن گ ب ن د ی ک ر و ا ن"
for i in test:
print(i)
# print unicode value
print(repr(i))
显然我的回答涉及 Python 2.7.x.