Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Text manipulation of a comma separated string in python

I want to read a textfile test.txt where the txt is in the format

'Jon, Stacy, Simon, ..., Maverick'

I’d want to save the string into test2.txt as

'Jon AS t1_Jon, Stacy AS t1_Stacy, Simon AS t1_Simon, ..., Maverick AS t1_Maverick'

It could be that there is a linebreak every now and then, I would want to ignore that. How would I do it in an efficient and easy way?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

PS: I couldn’t come up with a more fitting title, how would you name it?

>Solution :

One nice approach is to use the re module.

import re

s_in = 'apple, banana, orange,\n mango, guava'
words = re.split(r'[,\n]\s*',s_in)
s_out = ', '.join([f'{word} AS t1_{word}' for word in words])
print(s_out)

Result:

apple AS t1_apple, banana AS t1_banana, orange AS t1_orange, mango AS t1_mango, guava AS t1_guava
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading