简体   繁体   English

如何从较大的字符串字符中减去多个字符串?

[英]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.其中 TKTID,SITEID,VIPPASS,TKTSOW 将始终保持不变。

How to write the function in python 3.X to get the desire result ?如何在python 3.X中编写函数以获得所需的结果? I am new in python, search over the web, not able to find the solution, your kind help will be appreciated.我是 python 新手,在网上搜索,找不到解决方案,您的帮助将不胜感激。

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.切片的内容基于常量字符串/数据拆分事物中的位置。

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

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