简体   繁体   中英

How can I break up a file delimited by colons and semi-colons and place each element on a separate line/row?

I have a file with colon and semi-colon delimited string elements. They are email addresses formatted as such:

Tony Stark, <ironman@stark-tesla.com>; Clark Kent, <Ckent1@dailyplanet.com>; Peter Parker, <pparker1@spidy.com>; etc.

What I would like to do is separate each email using it's semi-colon and place it on it's own line or row:

eg

Tony Stark, <ironman@stark-tesla.com>
Clark Kent, <Ckent1@dailyplanet.com>
Peter Parker, <pparker1@spidy.com>

What is the most efficient way of accomplishing this?

Thanks

try

string="Tony Stark, <ironman@stark-tesla.com>; Clark Kent, <Ckent1@dailyplanet.com>; Peter Parker, <pparker1@spidy.com>"
for i in string.split("; "):
    print(i)

the.split method on strings returns an array containing the string split by the separator. The for loop loops through the split string and prints out each one on it's own line.

You can use this function to return the expected string. You play with test case to see what you exactly want.

代码片段

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