Tayen Madsen Tayen Madsen - 1 year ago 55
Python Question

How can I remove the portion of a string that comes before ; if there is more than one semicolon in the string?

If given the string "John Doe;Lodging;123;050617", which is a line within a file, I need to remove everything before and including the first semicolon, and after and including the last one. How can I do that without removing my ability to later split the remaining substring?

Previous questions do not address removing the portions once separated while retaining the portion needed for further use.

Answer Source

Stepwise for clarity:

string = "John Doe;Lodging;123;050617"
lst = string.split(';')
lst = lst[1:-1]
string = ';'.join(lst)
>>> 'Lodging;123'

As one line:

';'.join('John Doe;Lodging;123;050617'.split(';')[1:-1])
>>> 'Lodging;123'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download