Best way to retrieve variable values from a text file?

Referring on this question, I have a similar -but not the same- problem..

On my way, I'll have some text file, structured like:

var_a: 'home'
var_b: 'car'
var_c: 15.5

And I need that python read the file and then create a variable named var_a with value 'home', and so on.

Example:

#python stuff over here
getVarFromFile(filename) #this is the function that im looking for
print var_b
#output: car, as string
print var_c
#output 15.5, as number.

Is this possible, I mean, even keep the var type?

Notice that I have the full freedom to the text file structure, I can use the format I like if the one I proposed isn't the best.

EDIT: the ConfigParser can be a solution, but I don't like it so much, because in my script I'll have then to refer to the variables in the file with

config.get("set", "var_name")

But what I'll love is to refer to the variable directly, as I declared it in the python script...

There is a way to import the file as a python dictionary?

Oh, last thing, keep in mind that I don't know exactly how many variables would I have in the text file.

Edit 2: I'm very interested at stephan's JSON solution, because in that way the text file could be read simply with others languages (PHP, then via AJAX JavaScript, for example), but I fail in something while acting that solution:

#for the example, i dont load the file but create a var with the supposed file content
file_content = "'var_a': 4, 'var_b': 'a string'"
mydict = dict(file_content)
#Error: ValueError: dictionary update sequence element #0 has length 1; 2 is required
file_content_2 = "{'var_a': 4, 'var_b': 'a string'}"
mydict_2 = dict(json.dump(file_content_2, True))
#Error:
#Traceback (most recent call last):
#File "<pyshell#5>", line 1, in <module>
#mydict_2 = dict(json.dump(file_content_2, True))
#File "C:\Python26\lib\json\__init__.py", line 181, in dump
#fp.write(chunk)
#AttributeError: 'bool' object has no attribute 'write'

In what kind of issues can I fall with the JSON format? And, how can I read a JSON array in a text file, and transform it in a python dict?

P.S: I don't like the solution using .py files; I'll prefer .txt, .inc, .whatever is not restrictive to one language.


Asked by: Walter993 | Posted: 30-11-2021






Answer 1

But what i'll love is to refer to the variable direclty, as i declared it in the python script..

Assuming you're happy to change your syntax slightly, just use python and import the "config" module.

# myconfig.py:

var_a = 'home'
var_b = 'car'
var_c = 15.5

Then do

from myconfig import *

And you can reference them by name in your current context.

Answered by: Brooke477 | Posted: 01-01-2022



Answer 2

You can treat your text file as a python module and load it dynamically using imp.load_source:

import imp
imp.load_source( name, pathname[, file]) 

Example:

// mydata.txt
var1 = 'hi'
var2 = 'how are you?'
var3 = { 1:'elem1', 2:'elem2' }
//...

// In your script file
def getVarFromFile(filename):
    import imp
    f = open(filename)
    global data
    data = imp.load_source('data', '', f)
    f.close()

# path to "config" file
getVarFromFile('c:/mydata.txt')
print data.var1
print data.var2
print data.var3
...

Answered by: Wilson141 | Posted: 01-01-2022



Answer 3

Use ConfigParser.

Your config:

[myvars]
var_a: 'home'
var_b: 'car'
var_c: 15.5

Your python code:

import ConfigParser

config = ConfigParser.ConfigParser()
config.read("config.ini")
var_a = config.get("myvars", "var_a")
var_b = config.get("myvars", "var_b")
var_c = config.get("myvars", "var_c")

Answered by: Ryan207 | Posted: 01-01-2022



Answer 4

Load your file with JSON or PyYAML into a dictionary the_dict (see doc for JSON or PyYAML for this step, both can store data type) and add the dictionary to your globals dictionary, e.g. using globals().update(the_dict).

If you want it in a local dictionary instead (e.g. inside a function), you can do it like this:

for (n, v) in the_dict.items():
    exec('%s=%s' % (n, repr(v)))

as long as it is safe to use exec. If not, you can use the dictionary directly.

Answered by: Vanessa169 | Posted: 01-01-2022



Answer 5

The other solutions posted here didn't work for me, because:

  • i just needed parameters from a file for a normal script
  • import * didn't work for me, as i need a way to override them by choosing another file
  • Just a file with a dict wasn't fine, as I needed comments in it.

So I ended up using Configparser and globals().update()

Test file:

#File parametertest.cfg:
[Settings]
#Comments are no Problem
test= True
bla= False    #Here neither

#that neither

And that's my demo script:

import ConfigParser

cfg = ConfigParser.RawConfigParser()
cfg.read('parametertest.cfg')       # Read file

#print cfg.getboolean('Settings','bla') # Manual Way to acess them

par=dict(cfg.items("Settings"))
for p in par:
    par[p]=par[p].split("#",1)[0].strip() # To get rid of inline comments

globals().update(par)  #Make them availible globally

print bla

It's just for a file with one section now, but that will be easy to adopt.

Hope it will be helpful for someone :)

Answered by: Aida575 | Posted: 01-01-2022



Answer 6

Suppose that you have a file Called "test.txt" with:

a=1.251
b=2.65415
c=3.54
d=549.5645
e=4684.65489

And you want to find a variable (a,b,c,d or e):

ffile=open('test.txt','r').read()

variable=raw_input('Wich is the variable you are looking for?\n')

ini=ffile.find(variable)+(len(variable)+1)
rest=ffile[ini:]
search_enter=rest.find('\n')
number=float(rest[:search_enter])

print "value:",number

Answered by: Madaline777 | Posted: 01-01-2022



Answer 7

How reliable is your format? If the seperator is always exactly ': ', the following works. If not, a comparatively simple regex should do the job.

As long as you're working with fairly simple variable types, Python's eval function makes persisting variables to files surprisingly easy.

(The below gives you a dictionary, btw, which you mentioned was one of your prefered solutions).

def read_config(filename):
    f = open(filename)
    config_dict = {}
    for lines in f:
        items = lines.split(': ', 1)
        config_dict[items[0]] = eval(items[1])
    return config_dict

Answered by: Wilson760 | Posted: 01-01-2022



Answer 8

hbn's answer won't work out of the box if the file to load is in a subdirectory or is named with dashes.

In such a case you may consider this alternativeĀ :

exec open(myconfig.py).read()

Or the simpler but deprecated in python3Ā :

execfile(myconfig.py)

I guess Stephan202's warning applies to both options, though, and maybe the loop on lines is safer.

Answered by: Blake783 | Posted: 01-01-2022



Answer 9

A simple way of reading variables from a text file using the standard library:

# Get vars from conf file
var = {}
with open("myvars.txt") as conf:
        for line in conf:
                if ":" in line:
                        name, value = line.split(":")
                        var[name] = str(value).rstrip()
globals().update(var)

By default this reads each variable as a string. If you need them converted as literal variables(e.g lists/tuples/bool), then you would need to use the ast module:

import ast
ast.literal_eval(varname)

Answered by: Carlos153 | Posted: 01-01-2022



Answer 10

What you want appear to want is the following, but this is NOT RECOMMENDED:

>>> for line in open('dangerous.txt'):
...     exec('%s = %s' % tuple(line.split(':', 1)))
... 
>>> var_a
'home'

This creates somewhat similar behavior to PHP's register_globals and hence has the same security issues. Additionally, the use of exec that I showed allows arbitrary code execution. Only use this if you are absolutely sure that the contents of the text file can be trusted under all circumstances.

You should really consider binding the variables not to the local scope, but to an object, and use a library that parses the file contents such that no code is executed. So: go with any of the other solutions provided here.

(Please note: I added this answer not as a solution, but as an explicit non-solution.)

Answered by: Stuart999 | Posted: 01-01-2022



Answer 11

If your data has a regular structure you can read a file in line by line and populate your favorite container. For example:

Let's say your data has 3 variables: x, y, i.

A file contains n of these data, each variable on its own line (3 lines per record). Here are two records:

384
198
0
255
444
2

Here's how you read your file data into a list. (Reading from text, so cast accordingly.)

data = []
try:
    with open(dataFilename, "r") as file:
        # read data until end of file
        x = file.readline()
        while x != "":
            x = int(x.strip())    # remove \n, cast as int

            y = file.readline()
            y = int(y.strip())

            i = file.readline()
            i = int(i.strip())

            data.append([x,y,i])

            x = file.readline()

except FileNotFoundError as e:
    print("File not found:", e)

return(data)

Answered by: David770 | Posted: 01-01-2022



Similar questions

Retrieve variable after crash in python

My algorithm has run for a few hours, and when it was done it had to save the variable to a file. Though when writing to a file it crashed due to a memory exception... though the algorithm worked and the variable had the values I needed. Is there anyway to retrieve that variable at this point ? Btw I'm new to python, I'm fully aware what I'm asking is a bit strange. # -*- coding: utf-8 -*- from scipy import...


python - How to retrieve a variable from object to update it in Django?

I have a model Score, which has q-id's as columns. Ex: class Score(models.Model): group_id = models.ForeignKey(Group) name = models.CharField(max_length=60) gender = models.CharField(max_length=1,default = False ) q1 = models.DecimalField(max_digits=20,decimal_places=8) q2 = models.DecimalField(max_digits=20,decimal_places=8) . . q100 = models.DecimalField(max_digits=20,deci...


python - How to retrieve a variable name given its value

class type_name: def __init__(self, *field_names): self.x = field_names[0] self.y = field_names[1] self._fields = [x for x in field_names] def get_y(self): return self.y def __getitem__(self, ind): fstr = 'self.get_' + str(self._fields[ind]) #this would give me 5 i want self.y so I construct a string such as 'self.get_y()' #and then return eval on that st...


python - Retrieve part of string, variable length

I'm trying to learn how to use Regular Expressions with Python. I want to retrieve an ID number (in parentheses) in the end from a string that looks like this: "This is a string of variable length (561401)" The ID number (561401 in this example) can be of variable length, as can the text. "This is another string of variable length (99521199)" ...


Python retrieve variable from text

I'm trying to retrieve variables from a text file and pass those variables into python. Also in one text file i will have an content such as : Total Numbers: 22, Total Boxes: 33, Total Tickets: 62, Total Files: 107 Total Numbers: 2, Total Boxes: 53, Total Tickets: 92, Total Files: 105 Total Numbers: 3, Total Boxes: 13, Total Tickets: 22, Total Files: 137 Total Numbers: 42, Total Boxes: 38, Total Tickets: 62...


python - How to retrieve a GET variable with a hash in it

I have the following variable that is passed in a url: http://localhost:8000/diff/?Platform=Comcast&amp;PlatformID=7066191365225244112#S8 I need to be able to extract the following: Platform = Comcast PlatformID = 7066191365225244112#S8 However, django will escape...


python - How to retrieve an Enum key via variable

I'm new to python. Is it possible to get the value of an Enum key from a variable key? class Numbering(Enum): a=2 b=3 key = &quot;b&quot; print(Numbering.key) #the result I want is 3


python - Retrieve information about a variable itself?

In Python, everything is an object. What is the nature of variables? Explore it in an example: &gt;&gt;&gt; foo = 1 &gt;&gt;&gt; dir(foo) ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', ....


python - scrapy - How to retrieve a variable value using regex

I want to retrieve the value of the var modelCode. I made a regex function like this, but it doesn't work at all. I've posted the structure of the page below.Can somebody help me, please? regex2 = re.compile(r'"var modelCode"\s*:\s*(.+?\})', re.DOTALL) source_json3 = response.xpath("//script[contains(., 'if(pageTrackName == 'product detail' || pageTrackName == 'generic product details')')]/text...


python - Retrieve Variable From Class

I am trying to get the text variable from a QLineEdit widget that is created in a function within a class. Usually I would specify the class in which the variable was created for ex. var = classname.variable but this doesn't work in this case since the variable is created in a function in a class. Here is my code: from PyQt5.QtWidgets import QWi...


Is it possible to retrieve a JSON path from a variable in python?

I am writing a program that queries an API that responds with a JSON object. The JSON object is multilevel with several arrays and key value pairs. There is a fairly large number of items I would like to retrieve from each response, and typing out each path multiple times for each function is turning out to be time consuming and cluttering. I would like to store each JSON path in a dictionary to iterate over. A q...


python - How to retrieve all variable names within a netcdf using GDAL

I am struggling to find a way to retrieve metadata information from a FILE using GDAL. Specifically, I would like to retrieve the band names and the order in which they are stored in a given file (may that be a GEOTIFF or a NETCDF). For instance, if we follow the description within the GDAL documentation, we have the &quot;GetMetaData&quot; method from the gdal.Dataset (see


Retrieve variable after crash in python

My algorithm has run for a few hours, and when it was done it had to save the variable to a file. Though when writing to a file it crashed due to a memory exception... though the algorithm worked and the variable had the values I needed. Is there anyway to retrieve that variable at this point ? Btw I'm new to python, I'm fully aware what I'm asking is a bit strange. # -*- coding: utf-8 -*- from scipy import...


How to retrieve the contents of entry and text widget and assign it to a variable in tkinter using python

Well I have the following code which I wrote for creating a gui based email client using python.I used tkinter.I want that the text the user writes in the entry widget gets assigned to the variables sender,password,receiver,message. I have used entry widget for getting the values of sender,password,receiver and text widget for the message .How to do so?I tried many methods(like the get method,the textvariable method) for t...


python - How to retrieve a variable from object to update it in Django?

I have a model Score, which has q-id's as columns. Ex: class Score(models.Model): group_id = models.ForeignKey(Group) name = models.CharField(max_length=60) gender = models.CharField(max_length=1,default = False ) q1 = models.DecimalField(max_digits=20,decimal_places=8) q2 = models.DecimalField(max_digits=20,decimal_places=8) . . q100 = models.DecimalField(max_digits=20,deci...


python - How to retrieve a variable name given its value

class type_name: def __init__(self, *field_names): self.x = field_names[0] self.y = field_names[1] self._fields = [x for x in field_names] def get_y(self): return self.y def __getitem__(self, ind): fstr = 'self.get_' + str(self._fields[ind]) #this would give me 5 i want self.y so I construct a string such as 'self.get_y()' #and then return eval on that st...


python - Retrieve part of string, variable length

I'm trying to learn how to use Regular Expressions with Python. I want to retrieve an ID number (in parentheses) in the end from a string that looks like this: "This is a string of variable length (561401)" The ID number (561401 in this example) can be of variable length, as can the text. "This is another string of variable length (99521199)" ...


python - Is there any way to retrieve a local variable from a running function?

Desperate. Say we have the following: def main(): ALotOFCode list1 = [] list2 = [] while condition: # a lot of times where raw_input is used in this loop # e.g. x = raw_input('lots of steps to compute x') y = raw_input('lots of steps to compute y') list1 = list1.append(x) list2 = list2.append(y) stream.write({'x':list1,'y':list2}) #send new data point to plot.ly via raspberry pi ...


how to retrieve text from a variable stored in a list python

So I am trying to store multiple variables in a list, each variable contains text. After that, I would like to perform some analysis on the text which is stored inside the variable that is stored in the list. Code: my_list = list[] var_1 = 'I have lots of text' var_2 = 'I have even more text' var_3 = 'but wait there is more' my_list.append('var_1','var_2','var_3') for var in my_list: x = pack...


linux - How to get the variable of loop from Shell script to retrieve inside python script

Sorry, I am new in python and I just have basic of Shell script/sh. I have two question. 1) How to get the variable of the loop from bash to retrieve inside python script? 2) How to keep in max key-value from many files to the same dictionary? Those above questions are related. Explanation: The main script is in the Shell script, where they will loop some Shell command and then run Python script for many K...


Python retrieve variable from text

I'm trying to retrieve variables from a text file and pass those variables into python. Also in one text file i will have an content such as : Total Numbers: 22, Total Boxes: 33, Total Tickets: 62, Total Files: 107 Total Numbers: 2, Total Boxes: 53, Total Tickets: 92, Total Files: 105 Total Numbers: 3, Total Boxes: 13, Total Tickets: 22, Total Files: 137 Total Numbers: 42, Total Boxes: 38, Total Tickets: 62...


string - Retrieve information from txt file to use as a variable in a python file

I am dealing with a problem where I want to extract stock names from a text file to use in multiple python scripts, so that if I add one or more stocks I dont have to alter all the python files. My stocks.txt file is: AAPL GOOG TSLA If I import this the script saves A.txt, A.txt, P.txt, L.txt, G.txt etc. It takes every letter as seperate. How can I change my code so that i...


html - How can I retrieve the page title of a webpage using Python?

How can I retrieve the page title of a webpage (title html tag) using Python?


python - How to retrieve an element from a set without removing it?

Suppose the following: &gt;&gt;&gt; s = set([1, 2, 3]) How do I get a value (any value) out of s without doing s.pop()? I want to leave the item in the set until I am sure I can remove it - something I can only be sure of after an asynchronous call to another host. Quick and dirty: &gt;&gt;&gt; elem = s.pop() &gt;&gt;&gt; s.add(elem)


sql server - Python: Retrieve Image from MSSQL

I'm working on a Python project that retrieves an image from MSSQL. My code is able to retrieve the images successfully but with a fixed size of 63KB. if the image is greater than that size, it just brings the first 63KB from the image! The following is my code: #!/usr/bin/python import _mssql mssql=_mssql.connect('&lt;ServerIP&gt;','&lt;UserID&gt;','&lt;Password&gt;') mssql.select_db('&lt;Database...


python - How to retrieve the selected text from the active window

I am trying to create a simple open source utility for windows using Python that can perform user-defined actions on the selected text of the currently active window. The utility should be activated using a pre-defined keyboard shortcut. Usage is partially outlined in the following example: The user selects some text using the mouse or the keyboard (in any application window)


python - How can I retrieve last x elements in Django

I am trying to retrieve the latest 5 posts (by post time) In the views.py, if I try blog_post_list = blogPosts.objects.all()[:5] It retreives the first 5 elements of the blogPosts objects, how can I reverse this to retreive the latest ones? Cheers


python - Retrieve module object from stack frame

Given a frame object, I need to get the corresponding module object. In other words, implement callers_module so this works: import sys from some_other_module import callers_module assert sys.modules[__name__] is callers_module() (That would be equivalent because I can generate a stack trace in the function for this test case. The imports are there simply to make that example complete an...


How do I retrieve Hotmail contacts with python

How can I retrieve contacts from hotmail with python? Is there any example?


linux - How to retrieve the process start time (or uptime) in python

How to retrieve the process start time (or uptime) in python in Linux? I only know, I can call "ps -p my_process_id -f" and then parse the output. But it is not cool.


python - Retrieve the two highest item from a list containing 100,000 integers

How can retrieve the two highest item from a list containing 100,000 integers without having to sort the entire list first?


c++ - How do I retrieve program output in Python?

I'm not a Perl user, but from this question deduced that it's exceedingly easy to retrieve the standard output of a program executed through a Perl script using something akin to: $version = `java -version`; How would I go about getting the same end result in Python? Does t...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top