471,348 Members | 1,723 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 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 2937
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

Post your reply

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

Similar topics

14 posts views Thread by Peter Galfi | last post: by
4 posts views Thread by Xah Lee | last post: by
3 posts views Thread by George | last post: by
10 posts views Thread by Tyler | last post: by
3 posts views Thread by Paul Moore | last post: by
1 post views Thread by shrimpy | last post: by
reply views Thread by Ronak mishra | last post: by

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.