Python Forum
convert string into multiple columns in python3 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: convert string into multiple columns in python3 (/thread-29944.html)



convert string into multiple columns in python3 - VAN - Sep-26-2020

Suppose now I have the following string as below:

'DATA B - 22Aug18\nName Occupation Address Marry Teacher Cali Hobby listen music Sex F Sport swim Jack Doctor Texas Hobby Read book Sex M Sport Badminton \nDATA B - 22Aug18 \nName Occupation Address John student Cali Hobby Game Sex F Sport Tennis Susan freelancer NY Hobby Read book Sex M Sport Running'

And I need to get the following result:

Name Occupation Address Hobby Sex Sport
Mary teacher Cali listen music F swim
Jack Doctor Texas Read book M Badminton
John student Cali Game F Game
Susan freelancer NY Read book M Tennis

I can not how to describe this transformation. Anyone can help me? Thank you


RE: convert string into multiple columns in python3 - Larz60+ - Sep-26-2020

what have you tried so far?
Show your attempt no matter how terrible you think it is.


RE: convert string into multiple columns in python3 - scidam - Sep-26-2020

It seems that "value per column" rule doesn't always true for your data (e.g. first row: "listen music" (2 words); third row: "Game" (1 word)). You will probably need to define a set of allowed addresses (in advance) to parse such semi-structured data. "Sex" value could be a delimiter (only "F", "M" are allowed).