Reputation: 283
I have a text file, params.txt
say, that is of the following form (ignore the colour formatting, this is a plain text file not python code):
Lx = 512 Ly = 512
g = 400
================ Dissipation =====================
nupower = 8 nu = 0
alphapower = -0 alpha = 0
================ Timestepping =========================
SOMEFLAG = 1
SOMEOTHERFLAG = 4
dt = 2e-05
[...and so on]
i.e. the variables are separated by their values by =
, the values are a mixture of ints, floats, and scientific notation, there are sometimes two variable/value pairs on a line separated by a single space, and there are headings of the form
================ HeadingToBeDiscarded ================
In python, how do I read the text file, and automatically in my python script create the same variables and assign them the same values as are in the file?
The format of the file will be identical each time so brute forcing would be possible but I'm sure there's an elegant python/regex solution (but I'm new to python and have barely ever regexed!)
Upvotes: 0
Views: 751
Reputation: 283
For future readers, another solution is to use exec()
to run the appropriately chopped-up strings from params.txt
as code, to assign the variables with the values given:
with open('params.txt', 'r') as infile:
for line in infile:
splitline = line.strip().split(' ')
for i, word in enumerate(splitline):
if word == '=':
# DANGER! Don't use this unless you completely trust the content of params.txt!
exec(splitline[i-1] + splitline[i] + splitline[i+1])
This avoids having to parse the file, create a dictionary, print out a .py file and then reading in the .py file, as per Matiiss's solution.
I have posted another question here asking whether or not this is actually a good idea.
EDIT: it's not a great idea as exec()
is a security risk, but probably ok in code that's only ever run locally and does not face the outside world. In particular if nobody can ever write malicious code in params.txt
that exec()
would then execute.
Upvotes: 0
Reputation: 6156
now if you want to (hardcode?) data in .txt
file to .py
file you should use something like this:
temp_list = []
with open("params.txt") as file:
while True:
line = file.readline()
line = line.strip()
value = line.split(' ')
for i, word in enumerate(value):
if word == '=':
var = f'{value[i-1]} = {value[i+1]}'
temp_list.append(var)
if not line:
break
with open('sets.py', 'w') as f:
f.write('\n'.join(temp_list))
this will create a new python file named sets.py
(you can change name) and store all values from text file to .py file. Now to use these values first make sure that sets.py
is in the same directory as your main python scipt and then do from sets import *
now you will be able to acces any of those values by just typing its name and it will be recognized. try it out
Upvotes: 1
Reputation: 6156
improved script of one of the answers that can detect int, float and str
def getVariables():
with open("params.txt") as file:
variables = {}
while True:
line = file.readline()
line = line.strip()
value = line.split(' ')
for i, word in enumerate(value):
if word == '=':
try:
variables[str(value[i-1])] = int(value[i+1])
except ValueError:
try:
variables[str(value[i-1])] = float(value[i+1])
except ValueError:
variables[str(value[i-1])] = (value[i+1])
if not line:
break
return variables
Upvotes: 0
Reputation: 36
This should be doable with dictionary's I think.
something like this:
def getVariables():
with open("filename.txt",'r') as file:
variables = {}
while True:
line = file.readline()
line = line.strip()
value = line.split(' ')
for i, word in enumerate(value):
if word == '=':
variables[str(value[i-1])] = value[i+1]
if not line:
break
return variables
this leaves an output in the form of a dictionary with as key: the variable name and with the value: the variable itself. Like this:
variables = {'Lx' : '512', 'Ly' : '512', 'nupower' : '8', 'nu' : '0'}
I do not know a way of implementing some way of detecting if it is an int or float...
Upvotes: 0
Reputation: 88
you should probably not store it this way would be my advice.
If it isnt even considered for humans to read use pickle to store python objects.
If it is supposed to be readable/editable to humans i would suggest csv files or something in that nature
Upvotes: 0