I am trying to use the contents of a text file as Python code
e.g. if I write
import sys
in a text file, can I read that as a string and then turn it into Python code or is there another way to do it?
thanks
15 3090
I am trying to use the contents of a text file as Python code
e.g. if I write
import sys
in a text file, can I read that as a string and then turn it into Python code or is there another way to do it?
thanks
Python script (.py & .pyw) files ARE text files. They are read by the compiler an turned into byte code (.pyc) files which are run by the execution engine. All parts of this process are exposed in the library modules.
oops sorry I guess I wasn't too precise.
What I mean is, if I write a Python program that opens a text file and reads through the contents, I want to use the contents of that text file to access some constants.
e.g. I have defined more than 50 constants and when I read through the text file opened by my Python programme and find a string matching the name of one of my constant, I want to use that constant.
so in my script, I've written
porta = 100
and when i see 'porta' in the text file i want to
print porta
I tried using if statements but with 50+ of them I don't think that that's the best option.
I'm sorry if this still sounds confusing. I'm still really new to this.
cheers
bvdet 2,851
Expert Mod 2GB
oops sorry I guess I wasn't too precise.
What I mean is, if I write a Python program that opens a text file and reads through the contents, I want to use the contents of that text file to access some constants.
e.g. I have defined more than 50 constants and when I read through the text file opened by my Python programme and find a string matching the name of one of my constant, I want to use that constant.
so in my script, I've written
porta = 100
and when i see 'porta' in the text file i want to
print porta
I tried using if statements but with 50+ of them I don't think that that's the best option.
I'm sorry if this still sounds confusing. I'm still really new to this.
cheers
This can be accomplished quite easily: - def parseData(fn, keyList):
-
# initialize dictionary
-
dataDict = {}
-
-
# open file for reading
-
f = open(fn)
-
-
for line in f:
-
dataList = [s.strip() for s in line.split('=')]
-
if dataList[0] in keyList:
-
# Convert data to integer
-
dataDict[dataList[0]] = int(dataList[1])
-
f.close()
-
return dataDict
-
-
# Define a list the keywords to extract from the file
-
keyList = ['porta', 'long', 'high']
-
-
fn = 'your_file'
-
-
dd = parseData(fn, keyList)
-
-
if len(dd):
-
for key in dd:
-
print '%s = %s' % (key, dd[key])
-
else:
-
print 'The data you requested does not exist!'
-
-
# Update the global name space
-
# This is not necessary since the data is available in the dictionary
-
globals().update(dd)
-
for item in keyList:
-
print eval(item)
This can be accomplished quite easily: - <snip>
-
-
# Update the global name space
-
# This is not necessary since the data is available in the dictionary
-
globals().update(dd)
-
for item in keyList:
-
print eval(item)
I remember this: This is how it all started when you first joined, asking if updating the globals dictionary had any drawbacks. It's cool to see it in use again. Thanks.
bvdet 2,851
Expert Mod 2GB
I remember this: This is how it all started when you first joined, asking if updating the globals dictionary had any drawbacks. It's cool to see it in use again. Thanks.
Good memory Barton! It seems like a long time ago. I have learned a lot since then.
oops sorry I guess I wasn't too precise.
What I mean is, if I write a Python program that opens a text file and reads through the contents, I want to use the contents of that text file to access some constants.
e.g. I have defined more than 50 constants and when I read through the text file opened by my Python programme and find a string matching the name of one of my constant, I want to use that constant.
so in my script, I've written
porta = 100
and when i see 'porta' in the text file i want to
print porta
I tried using if statements but with 50+ of them I don't think that that's the best option.
I'm sorry if this still sounds confusing. I'm still really new to this.
cheers
I realize that this is an external file, but, if you have the opportunity, name it with the .py extension (using strictly python syntax in the file) and use - from variables import * # this syntax requires that the file be in a directory listed in sys.path
Good memory Barton! It seems like a long time ago. I have learned a lot since then.
"Good morning" to you, too. But it looks like it's closer to 3:00 PM your time - your post time.
Yep. This has been a grand learning experience for me, as well. It has been a real pleasure having your input on these boards!
I hope you are not as crazy-busy as I have been. Take care,
Barton
This can be accomplished quite easily: - def parseData(fn, keyList):
-
# initialize dictionary
-
dataDict = {}
-
-
# open file for reading
-
f = open(fn)
-
-
for line in f:
-
dataList = [s.strip() for s in line.split('=')]
-
if dataList[0] in keyList:
-
# Convert data to integer
-
dataDict[dataList[0]] = int(dataList[1])
-
f.close()
-
return dataDict
-
-
# Define a list the keywords to extract from the file
-
keyList = ['porta', 'long', 'high']
-
-
fn = 'your_file'
-
-
dd = parseData(fn, keyList)
-
-
if len(dd):
-
for key in dd:
-
print '%s = %s' % (key, dd[key])
-
else:
-
print 'The data you requested does not exist!'
-
-
# Update the global name space
-
# This is not necessary since the data is available in the dictionary
-
globals().update(dd)
-
for item in keyList:
-
print eval(item)
Thanks for the reply.
I could sort of see what's happening here. What it's doing is parsing a text file in which there are constants declared with their values seperated by an '=' sign right? That is helpful although I still have some problem connecting all the dots.
What I have is a module with all the constants defined (written by another of my team members) and I do use the code -
from variables import * # this syntax requires that the file be in a directory listed in sys.path
to get access to it.
Let's assume that the variables in them are used to select a device to use i.e. DeviceA = 3 and thus DeviceA would be used whenever we want to refer to that device rather than needing to remember the number 3.
So I've written a programme that is able to use many of the devices available. But I want to give the user some flexibility in choosing which out of the 50+ devices to use. I've done this by exporting the selection of a device to a text file
e.g. if he writes DeviceUsed = DeviceA in the text file , then I would conversely use the constant DeviceA in my programme.
My problem of course is that I will only read it as a string 'DeviceA' whilst the constant from the imported module is called DeviceA and has a value 3. The simplest solution I can think of is to do: -
if DeviceUsed == 'DeviceA':
-
DeviceConnect = DeviceA
-
which would be problematic as there are 50+ constants and growing.
Thanks for the replies guys.
cheers
Thanks for the reply.
I could sort of see what's happening here. What it's doing is parsing a text file in which there are constants declared with their values seperated by an '=' sign right? That is helpful although I still have some problem connecting all the dots.
What I have is a module with all the constants defined (written by another of my team members) and I do use the code -
from variables import * # this syntax requires that the file be in a directory listed in sys.path
to get access to it.
Let's assume that the variables in them are used to select a device to use i.e. DeviceA = 3 and thus DeviceA would be used whenever we want to refer to that device rather than needing to remember the number 3.
So I've written a programme that is able to use many of the devices available. But I want to give the user some flexibility in choosing which out of the 50+ devices to use. I've done this by exporting the selection of a device to a text file
e.g. if he writes DeviceUsed = DeviceA in the text file , then I would conversely use the constant DeviceA in my programme.
My problem of course is that I will only read it as a string 'DeviceA' whilst the constant from the imported module is called DeviceA and has a value 3. The simplest solution I can think of is to do: -
if DeviceUsed == 'DeviceA':
-
DeviceConnect = DeviceA
-
which would be problematic as there are 50+ constants and growing.
Thanks for the replies guys.
cheers
Ok, I don't have much experience with this eval() but it looks good like this:
Input file:
Program: - def parseData(fn, keyList):
-
# initialize dictionary
-
dataDict = {}
-
-
# open file for reading
-
f = open(fn)
-
-
for line in f:
-
dataList = [s.strip() for s in line.split('=')]
-
if dataList[0] in keyList:
-
# Convert data to integer
-
dataDict[dataList[0]] = dataList[1]
-
f.close()
-
return dataDict
-
-
# Define a list the keywords to extract from the file
-
keyList = ['porta', 'long', 'high', 'DeviceUsed']
-
-
porta = 3
-
-
fn = 'c:\\proba.txt'
-
-
dd = parseData(fn, keyList)
-
-
if len(dd):
-
for key in dd:
-
print '%s = %s' % (key, dd[key])
-
else:
-
print 'The data you requested does not exist!'
-
-
# Update the global name space
-
# Necessary for the last eval() to work
-
globals().update(dd)
-
for item in keyList:
-
print eval(item)
-
-
DeviceConnect = eval(DeviceUsed)
-
When you give the eval() the key from keyList it evaluates a string to it's content from the file (which I have changed to string!), but if you give it the name of the constant read from the file it takes it's value - 'porta', which after evaluation gives 3. I hope this works for you.
bvdet 2,851
Expert Mod 2GB
Thanks for the reply.
I could sort of see what's happening here. What it's doing is parsing a text file in which there are constants declared with their values seperated by an '=' sign right? That is helpful although I still have some problem connecting all the dots.
What I have is a module with all the constants defined (written by another of my team members) and I do use the code -
from variables import * # this syntax requires that the file be in a directory listed in sys.path
to get access to it.
Let's assume that the variables in them are used to select a device to use i.e. DeviceA = 3 and thus DeviceA would be used whenever we want to refer to that device rather than needing to remember the number 3.
So I've written a programme that is able to use many of the devices available. But I want to give the user some flexibility in choosing which out of the 50+ devices to use. I've done this by exporting the selection of a device to a text file
e.g. if he writes DeviceUsed = DeviceA in the text file , then I would conversely use the constant DeviceA in my programme.
My problem of course is that I will only read it as a string 'DeviceA' whilst the constant from the imported module is called DeviceA and has a value 3. The simplest solution I can think of is to do: -
if DeviceUsed == 'DeviceA':
-
DeviceConnect = DeviceA
-
which would be problematic as there are 50+ constants and growing.
Thanks for the replies guys.
cheers
You are on the right track. Importing the constants as a module eliminates the requirement of parsing a text file.
Try this: - >>> DeviceA = 3
-
>>> DeviceUsed = 'DeviceA'
-
>>> DeviceConnect = eval(DeviceUsed)
-
>>> DeviceConnect
-
3
-
>>>
Thanks all!!
eval() is exactly what solves everything.
Again thanks. You guys just help me take me baby steps into Python. Not in a gruesome way.
cheers
You are on the right track. Importing the constants as a module eliminates the requirement of parsing a text file.
Yes, but the point is the user will alter a text file to configure the program, or I am mistaken? Which will contain lines like:
So you just need a keyList that contains all constants that the user will change, like DeviceUsed.
bvdet 2,851
Expert Mod 2GB
Yes, but the point is the user will alter a text file to configure the program, or I am mistaken? Which will contain lines like:
So you just need a keyList that contains all constants that the user will change, like DeviceUsed.
I thought so at first, but when the OP referred to the file as a module, I became unsure.
Yes, but the point is the user will alter a text file to configure the program, or I am mistaken? Which will contain lines like:
So you just need a keyList that contains all constants that the user will change, like DeviceUsed.
Yeap that's exactly what I wanted to do. The line 'DeviceUsed=DeviceA' is in a text file, but within a module that I imported DeviceA is already defined as a certain integer value so now I just read the string 'DeviceUsed=DeviceA', parse it and put 'DeviceA' in the argument field of eval().
Thanks again guys!
cheers
Hi
I would be grateful if any1 can help me to solve my problem
I am writing a code on python.I am very new to it and I need your help my freinds. the code should
1) read some files and alter their values
2) should call another software and that software will run the produced files
3) the software it self will give a result file
4) code should read its output and calculate a misfit between the output and data from another file that contain real and observed data for that problem
what we generally want is to make the predctions better with ant colony method, if anyone knows anythinh about ACO.
Can anyone help me in this area
please send me your comments to yasin_reservoir@yahoo.com
Thank you all 4 ur help
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Peter Galfi |
last post by:
Hi!
I am looking for a library in Python that would read PDF files and I could extract information from the PDF with it. I have searched with google, but only found libraries that can be used to...
|
by: Xah Lee |
last post by:
# -*- coding: utf-8 -*-
# Python
# to open a file and write to file
# do
f=open('xfile.txt','w')
# this creates a file "object" and name it f.
# the second argument of open can be
|
by: travis ray |
last post by:
Hi,
I have an extension in which a file object is created in python and
passed down to a c extension which attempts to read from it or write
to it. Writing to the file pointer seems to work...
|
by: George |
last post by:
Hi,
I have exported some registry-keys using Regedit to a number of
..reg-files. I can open these files using any text editor. Now I wanted
to write a simple Python script to concatenate all...
|
by: Tyler |
last post by:
Hello All:
After trying to find an open source alternative to Matlab (or IDL), I
am currently getting acquainted with Python and, in particular SciPy,
NumPy, and Matplotlib. While I await the...
|
by: Paul Moore |
last post by:
I'd like to write some scripts to analyze and manipulate my music
files. The files themselves are in MP3 and FLAC format (mostly MP3,
but FLAC where I ripped original CDs and wanted a lossless...
|
by: shrimpy |
last post by:
hi every one, i am new to python,
and coz i want to write a handy command for my linux machine, to find
a word in all the files which are under the current folder.
the code is half done, but...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |