Parse key value pairs in a text file

后端 未结 6 1314
無奈伤痛
無奈伤痛 2020-11-28 09:31

I am a newbie with Python and I search how to parse a .txt file. My .txt file is a namelist with computation informations like :

myfile.txt

6条回答
  •  伪装坚强ぢ
    2020-11-28 09:58

    If there are multiple comma-separated values on a single line, here's code to parse that out:

        res = {}                                                                                                                                                                                             
    
        pairs = args.split(", ")                                                                                                                                                                             
        for p in pairs:                                                                                                                                                                                      
            var, val = p.split("=")                                                                                                                                                                          
            res[var] = val                                                                                                                                                                                   
    

提交回复
热议问题