如何使用Tensorflow在Python中处理字符子字符串?
使用Tensorflow的’strings’模块中存在的”substr”方法可以使用字符子字符串。接下来将其转换为Numpy数组,然后显示。
我们将看到如何使用Python表示Unicode字符串,并使用Unicode等效项来操作这些字符串。首先,根据脚本检测将Unicode字符串分成标记,使用标准字符串操作的Unicode等效项。
我们使用Google Colaboratory来运行以下代码。Google Colab或Colaboratory可以在浏览器上运行Python代码,不需要配置,可以免费访问GPU(图形处理器)。Colaboratory是基于Jupyter Notebook构建的。
print("默认单位为字节")
print("当len为1时,返回单个字节")
tf.strings.substr(thanks, pos=7, len=1).numpy()
print("单位指定为UTF8_CHAR")
print("它占用4个字节")
print(tf.strings.substr(thanks, pos=7, len=1, unit='UTF8_CHAR').numpy())
代码来源: https://www.tensorflow.org/tutorials/load_data/unicode
更多Python相关文章,请阅读:Python 教程
输出
默认单位为字节
当len为1时,返回单个字节
单位指定为UTF8_CHAR
它占用4个字节
b''
解释
- tf.strings.substr操作需要”unit”参数。
- 然后使用此参数确定”pos”和”len”参数将包含哪种偏移量。
极客教程