How to split each individual value between two string in Python

  • A+
Category:Languages
variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4" 

How can I split each value which is between (; and ~)?

The result will be like CREATEDBY,CREATEDBYNAME,CREATEDBYYOMINAME,...

I have tried the below, but it's giving the first occurrence.

variable[variable.find(";")+1:myString.find("~")] 

How do I get the list of strings by using the split?

 


Using str.split

Ex:

variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"  for i in variable.strip(";").split(";"):     print(i.split("~", 1)[0]) #or print([i.split("~", 1)[0] for i in variable.strip(";").split(";")]) 

Output:

CREATEDBY CREATEDBYNAME CREATEDBYYOMINAME CREATEDON CREATEDONUTC  ['CREATEDBY', 'CREATEDBYNAME', 'CREATEDBYYOMINAME', 'CREATEDON', 'CREATEDONUTC'] 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: