简体   繁体   English

从 python 字符串中提取值信息

[英]extract value information from python string

I have a string output and I would like to extract the str_data out.我有一个字符串 output ,我想提取 str_data 。 That is the value in str_data.那就是 str_data 中的值。 Currently I'm using the below code but I think it can be improved on.目前我正在使用下面的代码,但我认为它可以改进。 The below code does not work well with str_data=[''] and str_data=['L'm'] as it return list/index out of range error.下面的代码不适用于str_data=['']str_data=['L'm']因为它返回列表/索引超出范围错误。 str_data contains language information, so it could be empty or contain words like it's . str_data 包含语言信息,因此它可以为空或包含类似it's单词。 Anyway to improve this?无论如何要改善这一点? Thanks谢谢

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={}

I would like to extract out:我想提取:

example_1_result = 'ขอคุยด้วยหน่อย'
example_2_result = ''
example_3_result = 'L'm'

Assuming TensorFlow(...) is a string, that will always be formatted with the same arguments, then something like this will work:假设TensorFlow(...)是一个字符串,它将始终使用相同的 arguments 进行格式化,那么这样的事情将起作用:

final = result.split(",")[1].split("str_data=")[1].replace("[","").replace("]","")

Here's a breakdown:这是一个细分:

Example input:示例输入:

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
"'ขอคุยด้วยหน่อย'"

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

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