【发布时间】:2021-03-25 19:59:32
【问题描述】:
我使用tf.strings.unicode_split 分割文本字符。
当我使用英文字符时,它工作正常
example_texts = ['hello world']
chars = tf.strings.unicode_split(example_texts, input_encoding='UTF-8')
print(chars)
<tf.RaggedTensor [[b'h', b'e', b'l', b'l', b'o', b' ', b'w', b'o', b'r', b'l', b'd']]>
但如果更改为 UTF-8 Unicode 字符,它就不能像英文字符那样工作
example_texts = ['سلام دنیا']
chars = tf.strings.unicode_split(example_texts, input_encoding='UTF-8')
print(chars)
<tf.RaggedTensor [[b'\xd8\xb3', b'\xd9\x84', b'\xd8\xa7', b'\xd9\x85', b' ', b'\xd8\xaf',
b'\xd9\x86', b'\xdb\x8c', b'\xd8\xa7']]>
谢谢。
【问题讨论】:
-
对我来说看起来是正确的。你期待的是什么?
-
我期待这样的分裂波斯字符:س ل ا م 。不是这样的:xd8\xb3 xd8\x84 ...
-
显然,字符被编码为UTF-8。在英文示例中,同样的情况也发生了(字符是字节字符串——参见字节前缀),您似乎并不介意。试试这个:
b'\xd8\xb3'.decode('utf8') == 'س',就像b'h'.decode('utf8') == 'h'。 -
我试过 ".decode('utf-8') " 成功了,谢谢
标签: python tensorflow unicode utf-8 python-unicode