在python中拆分动态字符串



是否可以从这个字符串中获取一些键值

"最佳情况";

A = '<main=NewStuff stuff named This_is_dynamic/Dynamic_Meta_Info with ID 12345 in 88981>'

def get_important_values(A):

newlist = []

test = A.split(" ")
print (test)

count = 4
while True:
if count == 100:
print ("COunt Breakout")
break

if test[count] == "with":
print (count)
if count != 4:
print (type(test[5]))
test[3:count-1] = [''.join(map(str, test[3:count-1]))]

else:
pass # do nothing
break
else:
pass

count +=1

print (test)

KeyValue1 = test[0]
KeyValue2 = test[3]
KeyValue3 = test[6]
keyValue4 = test[9]
return KeyValue1, KeyValue2, KeyValue3, KeyValue4

A = '<main=NewStuff stuff named This_is_dynamic/Dynamic_Meta_Info with ID 12345 in 88981>'
get_important_values(A)

但如果我按照字符串指示尝试,我会遇到一些问题

A = '<main=NewStuff stuff named This is dyn a mi  c/Dynamic_Meta_Info with ID 12345 in 88981>'
  1. 我希望索引3像这个"This is dyn a mi c/Dynamic_Meta_Info",但我得到的是这个"Thisisdynami"我失去了所有的空间,也没有得到整个

  2. 有更好的方法做这个吗

def get_important_values(A : str):

# first I used inverted stings to the first and and last value
test = A.split(" ")
if test[4] != "with":
ss = re.search(r'stuff named (.*?) with ID',A).group(1)
else:
ss = test[3]
Value1 = test[0]
Value 2 = ss.rsplit("/", 1)
Value 3 = ElementName_Type[0]
Value Address = test[-1]
return Value1, Value 2

A = '<main=NewStuff stuff named This_is_dynamic/Dynamic_Meta_Info with ID 12345 in 88981>'
get_important_values(A)

最新更新