Parse key value pairs in a text file

后端 未结 6 1283
無奈伤痛
無奈伤痛 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:59

    Dict comprehensions (PEP 274) can be used for a shorter expression (60 characters):

    d = {k:float(v) for k, v in (l.split('=') for l in open(f))}
    

    EDIT: shortened from 72 to 60 characters thanks to @jmb suggestion (avoid .readlines()).

提交回复
热议问题