473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reading Python code from text files

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
Jul 13 '07 #1
15 3090
bartonc
6,596 Expert 4TB
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.
Jul 13 '07 #2
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
Jul 14 '07 #3
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:
Expand|Select|Wrap|Line Numbers
  1. def parseData(fn, keyList):
  2.     # initialize dictionary
  3.     dataDict = {}
  4.  
  5.     # open file for reading
  6.     f = open(fn)
  7.  
  8.     for line in f:
  9.         dataList = [s.strip() for s in line.split('=')]
  10.         if dataList[0] in keyList:
  11.             # Convert data to integer
  12.             dataDict[dataList[0]] = int(dataList[1])
  13.     f.close()
  14.     return dataDict
  15.  
  16. # Define a list the keywords to extract from the file
  17. keyList = ['porta', 'long', 'high']
  18.  
  19. fn = 'your_file'
  20.  
  21. dd = parseData(fn, keyList)
  22.  
  23. if len(dd):
  24.     for key in dd:
  25.         print '%s = %s' % (key, dd[key])
  26. else:
  27.     print 'The data you requested does not exist!'
  28.  
  29. # Update the global name space 
  30. # This is not necessary since the data is available in the dictionary
  31. globals().update(dd)
  32. for item in keyList:
  33.     print eval(item)
Jul 14 '07 #4
bartonc
6,596 Expert 4TB
This can be accomplished quite easily:
Expand|Select|Wrap|Line Numbers
  1. <snip>
  2.  
  3. # Update the global name space 
  4. # This is not necessary since the data is available in the dictionary
  5. globals().update(dd)
  6. for item in keyList:
  7.     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.
Jul 14 '07 #5
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.
Jul 14 '07 #6
bartonc
6,596 Expert 4TB
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
Expand|Select|Wrap|Line Numbers
  1. from variables import *  # this syntax requires that the file be in a directory listed in sys.path
Jul 14 '07 #7
bartonc
6,596 Expert 4TB
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
Jul 14 '07 #8
This can be accomplished quite easily:
Expand|Select|Wrap|Line Numbers
  1. def parseData(fn, keyList):
  2.     # initialize dictionary
  3.     dataDict = {}
  4.  
  5.     # open file for reading
  6.     f = open(fn)
  7.  
  8.     for line in f:
  9.         dataList = [s.strip() for s in line.split('=')]
  10.         if dataList[0] in keyList:
  11.             # Convert data to integer
  12.             dataDict[dataList[0]] = int(dataList[1])
  13.     f.close()
  14.     return dataDict
  15.  
  16. # Define a list the keywords to extract from the file
  17. keyList = ['porta', 'long', 'high']
  18.  
  19. fn = 'your_file'
  20.  
  21. dd = parseData(fn, keyList)
  22.  
  23. if len(dd):
  24.     for key in dd:
  25.         print '%s = %s' % (key, dd[key])
  26. else:
  27.     print 'The data you requested does not exist!'
  28.  
  29. # Update the global name space 
  30. # This is not necessary since the data is available in the dictionary
  31. globals().update(dd)
  32. for item in keyList:
  33.     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
Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. if DeviceUsed == 'DeviceA':
  2.     DeviceConnect = DeviceA
  3.  
which would be problematic as there are 50+ constants and growing.

Thanks for the replies guys.

cheers
Jul 15 '07 #9
elbin
27
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
Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. if DeviceUsed == 'DeviceA':
  2.     DeviceConnect = DeviceA
  3.  
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:
Expand|Select|Wrap|Line Numbers
  1. DeviceUsed = porta
  2.  
Program:
Expand|Select|Wrap|Line Numbers
  1. def parseData(fn, keyList):
  2.     # initialize dictionary
  3.     dataDict = {}
  4.  
  5.     # open file for reading
  6.     f = open(fn)
  7.  
  8.     for line in f:
  9.         dataList = [s.strip() for s in line.split('=')]
  10.         if dataList[0] in keyList:
  11.             # Convert data to integer
  12.             dataDict[dataList[0]] = dataList[1]
  13.     f.close()
  14.     return dataDict
  15.  
  16. # Define a list the keywords to extract from the file
  17. keyList = ['porta', 'long', 'high', 'DeviceUsed']
  18.  
  19. porta = 3
  20.  
  21. fn = 'c:\\proba.txt'
  22.  
  23. dd = parseData(fn, keyList)
  24.  
  25. if len(dd):
  26.     for key in dd:
  27.         print '%s = %s' % (key, dd[key])
  28. else:
  29.     print 'The data you requested does not exist!'
  30.  
  31. # Update the global name space 
  32. # Necessary for the last eval() to work
  33. globals().update(dd)
  34. for item in keyList:
  35.     print eval(item)
  36.  
  37. DeviceConnect = eval(DeviceUsed)
  38.  
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.
Jul 15 '07 #10
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
Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. if DeviceUsed == 'DeviceA':
  2.     DeviceConnect = DeviceA
  3.  
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:
Expand|Select|Wrap|Line Numbers
  1. >>> DeviceA = 3
  2. >>> DeviceUsed = 'DeviceA'
  3. >>> DeviceConnect = eval(DeviceUsed)
  4. >>> DeviceConnect
  5. 3
  6. >>> 
Jul 15 '07 #11
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
Jul 15 '07 #12
elbin
27
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:
Expand|Select|Wrap|Line Numbers
  1. DeviceUsed = DeviceA
So you just need a keyList that contains all constants that the user will change, like DeviceUsed.
Jul 15 '07 #13
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:
Expand|Select|Wrap|Line Numbers
  1. DeviceUsed = DeviceA
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.
Jul 15 '07 #14
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:
Expand|Select|Wrap|Line Numbers
  1. DeviceUsed = DeviceA
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
Jul 16 '07 #15
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
Jan 11 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

14
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...
4
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
0
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...
3
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...
10
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...
3
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...
1
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...
0
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$) { } ...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.