简体   繁体   English

如何在Tensorflow中将字节转换为字符串

[英]How can I convert bytes to string In Tensorflow

I have a list of bytes and I want to convert it to a list of strings, in python I use this decode function: 我有一个字节列表,我想将其转换为字符串列表,在python中,我使用以下解码函数:

x=[b'\xd8\xa8\xd9\x85\xd8\xb3\xd8\xa3\xd9\x84\xd8\xa9',
    b'\xd8\xa5\xd9\x86\xd8\xb4\xd8\xa7\xd8\xa1',
    b'\xd9\x82\xd8\xb6\xd8\xa7\xd8\xa1',
    b'\xd8\xac\xd9\x86\xd8\xa7\xd8\xa6\xd9\x8a',
    b'\xd8\xaf\xd9\x88\xd9\x84\xd9\x8a'] 
y=[ a.decode("utf-8") for a in x]

How can I get the same result in Tensorflow? 如何在Tensorflow中获得相同的结果?

thank you 谢谢

You can use tf.compat.as_str_any() . 您可以使用tf.compat.as_str_any()

for i in x:
    print(tf.compat.as_str_any(i))

will print list items as python strings. 将列表项打印为python字符串。 No need to use session. 无需使用会话。

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

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