Python Forum

Full Version: convert string into multiple columns in python3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
what have you tried so far?
Show your attempt no matter how terrible you think it is.
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).