Home > front end >  Find specific word and print till newline in Python
Find specific word and print till newline in Python

Time:02-10

I have a string and I want to match something that start with specific word and end with newline. How can this be done?

Website:https://www.abc1.xyz/
Product:Apparal
TM Link:https://www.abc2.xyz/
Other Link:https://www.abc3.xyz/

I want to extract [Website,Product,TM Link,Other Link] and save this in a CSV. I am new to writing regular expression, I was wondering if anyone had a good solution to this that would be awesome!

CodePudding user response:

No need for regex, two splits do the trick here too

s = """Website:https://www.abc1.xyz/
Product:Apparal
TM Link:https://www.abc2.xyz/
Other Link:https://www.abc3.xyz/"""

res = [l.split(':')[0] for l in [line for line in s.split('\n')]]


with open('file.csv', 'w') as f:
    f.write(','.join(res))

If you want to get the part after the first semicolon:

s = """Website:https://www.abc1.xyz/
Product:Apparal
TM Link:https://www.abc2.xyz/
Other Link:https://www.abc3.xyz/"""

res = [l.split(':', maxsplit=1)[1] for l in [line for line in s.split('\n')]]


with open('file.csv', 'w') as f:
    f.write(','.join(res))
  • Related