Parse key value pairs in a text file

73,472

Solution 1

I suggest storing the values in a dictionary instead of in separate local variables:

myvars = {}
with open("namelist.txt") as myfile:
    for line in myfile:
        name, var = line.partition("=")[::2]
        myvars[name.strip()] = float(var)

Now access them as myvars["var1"]. If the names are all valid python variable names, you can put this below:

names = type("Names", [object], myvars)

and access the values as e.g. names.var1.

Solution 2

I personally solved this by creating a .py file that just contains all the parameters as variables - then did:

include PARAMETERS.py

in the program modules that need the parameters.

It's a bit ugly, but VERY simple and easy to work with.

Solution 3

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()).

Solution 4

As @kev suggests, the configparser module is the way to go.

However in some scenarios (a bit ugly, I admit) but very simple and effective way to do to this is to rename myfile.txt to myfile.py and do a from myfile import * (after you fix the typo var 0 -> var0)

However, this is very insecure, so if the file is from an external source or can be written by a malicious attacker, use something that validates the data instead of executing it blindly.

Share:
73,472

Related videos on Youtube

Vincent
Author by

Vincent

Researcher, astrophysicist, computer scientist, programming language expert, software architect and C++ standardization committee member. LinkedIn: https://www.linkedin.com/in/vincent-reverdy

Updated on May 11, 2022

Comments

  • Vincent
    Vincent almost 2 years

    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

    var0 = 16
    var1 = 1.12434E10
    var2 = -1.923E-3
    var3 = 920

    How to read the values and put them in myvar0, myvar1, myvar2, myvar3 in python?

  • snow6oy
    snow6oy over 8 years
    In my case he use of strip() on name was redundant, but the newline on var caused a problem. So what worked better for me was myvars[name] = float(var.strip())
  • Aryeh Armon
    Aryeh Armon over 7 years
    what happens if the file cas comments '# this is a comment line'
  • Francesco Frassinelli
    Francesco Frassinelli over 5 years
    @snow6oy float(val) and float(val.strip()) are equivalent
  • Jmb
    Jmb over 5 years
    You can also remove the .readlines() for an even shorter (and faster) expression
  • Daniel Inbaraj
    Daniel Inbaraj about 2 years
    If you have comments '# this is a comment line' in your file, just check the empty condition in you var variable and assign. for example, add the below if condition before assigning. this would ignore the commented line, if var: myvars[name.strip()] = float(var) Note: ensure you don't have any "=" symbol in your commented line