Home>

there is a string by type a= '# player rank status 1 lenny_bezkurov x is now playing 2 Mike_Deporto x not in the game 3 shon_blake ...' etc. Is it possible in any way to get a dictionary from a similar line? By type {'#': '1', 'player': 'Mike_Deporto' ...}

And what kind of dictionary will be the keys? Or do you need a dictionary from dictionaries?

pasporty2021-07-21 19:38:22
  • Answer # 1

    In general, through regular expressions, you can try to pull out data through Re.Findall .

    first pull the keys Keys. on mask, then players data Finds. . Then we combine everything in the dictionaries list COMB .

    Import Re
    a= '# player rank status 1 lenny_bezkurov x is now playing 2 Mike_Deporto x not in the game'
    KEYS= RE.FINDALL ('^ (\ s) ([\ w] +) ([\ w] +) ([\ w] +)', a)
    Finds= Re.Findall ('[[\ d] +) ([\ w] +) ([\ w] +) ([\ s \ d] +)', a)
    COMB= [DICT (Zip (* Keys, F)) for F in Finds]
    Print (* COMB, SEP= '\ N')
    

    Conclusion:

    {'#': '1', 'player': 'lenny_bezkurov', 'rank': 'x', 'Status': 'Now playing'}
    {'#': '2', 'player': 'mike_deporto', 'rank': 'x', 'status': 'not in the game'}
    
  • Answer # 2

    In general, through regular expressions, you can try to pull out data through Re.Findall .

    first pull the keys Keys. on mask, then players data Finds. . Then we combine everything in the dictionaries list COMB .

    Import Re
    a= '# player rank status 1 lenny_bezkurov x is now playing 2 Mike_Deporto x not in the game'
    KEYS= RE.FINDALL ('^ (\ s) ([\ w] +) ([\ w] +) ([\ w] +)', a)
    Finds= Re.Findall ('[[\ d] +) ([\ w] +) ([\ w] +) ([\ s \ d] +)', a)
    COMB= [DICT (Zip (* Keys, F)) for F in Finds]
    Print (* COMB, SEP= '\ N')
    

    Conclusion:

    {'#': '1', 'player': 'lenny_bezkurov', 'rank': 'x', 'Status': 'Now playing'}
    {'#': '2', 'player': 'mike_deporto', 'rank': 'x', 'status': 'not in the game'}