简体   繁体   中英

How to substract Multiple strings from a larger string character?

I have input data like this -

TKTID000;SITEIDNYL01101;VIPPASS123456;TKTSOWTEST

I need it in a form below -

TKTID = 0000
SITEID = NYL01101
VIPPASS = 123456
TKTSOW = TEST

where TKTID,SITEID,VIPPASS,TKTSOW will be always constant.

How to write the function in python 3.X to get the desire result ? I am new in python, search over the web, not able to find the solution, your kind help will be appreciated.

text1 = "TKTID000"
text2 = "SITEIDNYL01101"
text3 = "VIPPASS123456"
text4 = "TKTSOWTEST"

sht1 = "TKTID"
sht2 = "SITEID"
sht3 = "VIPPASS"
sht4 = "TKTSOW "


input1 =text1.replace(sht1,'')
input2 =text2.replace(sht2,'')
input3 =text2.replace(sht3,'')
input4 =text4.replace(sht4,'')  


print(input1)
print(input2)
print(input3)
print(input4)

Thanks in Advance !

In case your data input is always in the same order as your constants presented above you can do

data = "TKTID000;SITEIDNYL01101;VIPPASS123456;TKTSOWTEST"

constants = ["TKTID", "SITEID", "VIPPASS", "TKTSOW"]

for i,d in enumerate(data.split(";")):
    print(f"{constants[i]}={d[len(constants[i]):]}")

Output:

TKTID=000
SITEID=NYL01101
VIPPASS=123456
TKTSOW=TEST

This uses string slicing to slice the first part from the string. What to slice is based on the position in the constants string/data splitted thing.

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