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

Use Python to split a string and output each token into seperate lines?

$ echo '"a1","a2","a3"'|python3 -c "import sys; print('\n'.join(sys.stdin.read().splitlines()), sep='\n');"
"a1","a2","a3"

$ echo '"a1","a2","a3"'|python3 -c "import sys; [print(a, sep='\n') for a in sys.stdin.read().splitlines()];"
"a1","a2","a3"

$ echo '"a1","a2","a3"'|python3 -c "import sys,pprint; pprint.pprint('\n'.join(sys.stdin.read().splitlines()));"
'"a1","a2","a3"'

I have tried many different methods but none of them work for me.
I would like to print each token into a seperate line.

Question> How can I get the following results?

"a1"
"a2"
"a3"

Thank you

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

>Solution :

Split on comma instead.

print('\n'.join(sys.stdin.read().split(',')))
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