Parsing text within dictionary and separate into keys and values

Viewed 54

I want to parse some text within a dictionary, for example I want text with colons on the left and on the right those without colons. So that I can have those with colons as key and those without as values.

Here's the data:

x={0: {0: 'Name in home country:\n                        Kylian Mbappé Lottin\n                                                    Date of birth:\n                            Dec 20, 1998                                \n                                                    Place of birth:\n                            \n                                 Paris\xa0\xa0                            \n                                                                                                Age:\n                                22\n                                                                                            Height:\n                            1,78\xa0m\n                                                Citizenship:\n                            \n                                \xa0\xa0France                            \n                                            Position:\n                        \n                            attack - Centre-Forward                        \n                                                Foot:\n                            right\n                                                                    Player agent:\n                            \n                                                                    Relatives                                                            \n                                            \n                            Current club:\n                        \n                        \n                                                        Paris Saint-Germain                        \n                                                Joined:\n                            \n                                Jul 1, 2018                            \n                            Contract expires:\n                            Jun 30, 2022\n                                                                        Outfitter:\n                            Nike\n                        \n                                                Social-Media:'}}

Then convert to pandas dataframe:

x = pd.DataFrame(x)

Attempts I have tried:

x[0][0].replace('\n', '').split("/")[0]

Which removes the white spaces but I cannot select for specific text and there's lots of spaces.

I've also tried:

x[0][0].replace('\n', ',').split(',')

This works much better as I can actually index what I wanted however because Date of birth has a comma then I get split dates but I'd rather have a single. But also I get many empty indexed values that have just a space and comma.

for example:

data = {}
for j in range(0, len(x[0][0].replace('\n', ',').split(','))):
    for i in x[0]:
        print(i.replace('\n', ',').split(',')[j])

Although I'm unsure how to set this up to get an expected outcome like below because of the different split sizes.

Expected outcome:

{'Name in home country:':["Kylian Mbappé Lottin"],
'Date of birth:':["Dec 20, 1998"],
'Place of birth:':["Paris\xa0\xa0"]
...
...
}
1 Answers

You can use re.split to format your data and then use zip to create a dictionary:

import re
x={0: {0: 'Name in home country:\n                        Kylian Mbappé Lottin\n                                                    Date of birth:\n                            Dec 20, 1998                                \n                                                    Place of birth:\n                            \n                                 Paris\xa0\xa0                            \n                                                                                                Age:\n                                22\n                                                                                            Height:\n                            1,78\xa0m\n                                                Citizenship:\n                            \n                                \xa0\xa0France                            \n                                            Position:\n                        \n                            attack - Centre-Forward                        \n                                                Foot:\n                            right\n                                                                    Player agent:\n                            \n                                                                    Relatives                                                            \n                                            \n                            Current club:\n                        \n                        \n                                                        Paris Saint-Germain                        \n                                                Joined:\n                            \n                                Jul 1, 2018                            \n                            Contract expires:\n                            Jun 30, 2022\n                                                                        Outfitter:\n                            Nike\n                        \n                                                Social-Media:'}}
kv = re.split(r':?\s*\n\s*', x[0][0])
print(dict(zip(kv[::2], kv[1::2])))

Output:

{'Name in home country': 'Kylian Mbappé Lottin', 'Date of birth': 'Dec 20, 1998', 'Place of birth': 'Paris', 'Age': '22', 'Height': '1,78\xa0m', 'Citizenship': 'France', 'Position': 'attack - Centre-Forward', 'Foot': 'right', 'Player agent': 'Relatives', 'Current club': 'Paris Saint-Germain', 'Joined': 'Jul 1, 2018', 'Contract expires': 'Jun 30, 2022', 'Outfitter': 'Nike'}
Related