简体   繁体   中英

extract value information from python string

I have a string output and I would like to extract the str_data out. That is the value in 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 contains language information, so it could be empty or contain words like 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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