从 python 字符串中提取值信息
extract value information from python string
我有一个字符串输出,我想提取 str_data 出来。即str_data中的值。目前我正在使用下面的代码,但我认为它可以改进。以下代码不适用于 str_data=['']
和 str_data=['L'm']
,因为它 return list/index 超出范围错误。 str_data 包含语言信息,因此它可以为空或包含像 it's
这样的词。无论如何要改善这个?谢谢
right = result.split("str_data=['")[1]
final = right.split("'], extra='")[0]
Example 1:
result = TensorSet(tensors={'result': Tensor(shape=['5'], str_data=['ขอคุยด้วยหน่อย'], extra={})}, extra={}
Example 2:
result = TensorSet(tensors={'result': Tensor(shape=['102'], str_data=[''], extra={})}, extra={}
Example 3:
result = TensorSet(tensors={'result': Tensor(shape=[], str_data=['L'm'], extra={})}, extra={}
我要提取出来:
example_1_result = 'ขอคุยด้วยหน่อย'
example_2_result = ''
example_3_result = 'L'm'
假设 TensorFlow(...)
是一个字符串,它将始终使用相同的参数进行格式化,那么像这样的东西将起作用:
final = result.split(",")[1].split("str_data=")[1].replace("[","").replace("]","")
细目如下:
示例输入:
result = "TensorSet(tensors={'result': Tensor(shape=['5'], str_data=['ขอคุยด้วยหน่อย'], extra={})}, extra={})"
>>> result.split(",")[1]
" str_data=['ขอคุยด้วยหน่อย']"
>>> data = result.split(",")[1]
>>> data.split("str_data=")[1]
"['ขอคุยด้วยหน่อย']"
>>> content = data.split("str_data=")[1]
>>> content.replace("[","").replace("]","")
"'ขอคุยด้วยหน่อย'"
>>> final = content.replace("[","").replace("]","")
>>> final
"'ขอคุยด้วยหน่อย'"
我有一个字符串输出,我想提取 str_data 出来。即str_data中的值。目前我正在使用下面的代码,但我认为它可以改进。以下代码不适用于 str_data=['']
和 str_data=['L'm']
,因为它 return list/index 超出范围错误。 str_data 包含语言信息,因此它可以为空或包含像 it's
这样的词。无论如何要改善这个?谢谢
right = result.split("str_data=['")[1]
final = right.split("'], extra='")[0]
Example 1:
result = TensorSet(tensors={'result': Tensor(shape=['5'], str_data=['ขอคุยด้วยหน่อย'], extra={})}, extra={}
Example 2:
result = TensorSet(tensors={'result': Tensor(shape=['102'], str_data=[''], extra={})}, extra={}
Example 3:
result = TensorSet(tensors={'result': Tensor(shape=[], str_data=['L'm'], extra={})}, extra={}
我要提取出来:
example_1_result = 'ขอคุยด้วยหน่อย'
example_2_result = ''
example_3_result = 'L'm'
假设 TensorFlow(...)
是一个字符串,它将始终使用相同的参数进行格式化,那么像这样的东西将起作用:
final = result.split(",")[1].split("str_data=")[1].replace("[","").replace("]","")
细目如下:
示例输入:
result = "TensorSet(tensors={'result': Tensor(shape=['5'], str_data=['ขอคุยด้วยหน่อย'], extra={})}, extra={})"
>>> result.split(",")[1]
" str_data=['ขอคุยด้วยหน่อย']"
>>> data = result.split(",")[1]
>>> data.split("str_data=")[1]
"['ขอคุยด้วยหน่อย']"
>>> content = data.split("str_data=")[1]
>>> content.replace("[","").replace("]","")
"'ขอคุยด้วยหน่อย'"
>>> final = content.replace("[","").replace("]","")
>>> final
"'ขอคุยด้วยหน่อย'"