getting values of global variables?
Holger Jannsen
holger at phoenix-edv.netzservice.de
Wed Jul 14 05:59:29 EDT 1999
Hi,
ok, sounds easy, but I get a little confused...;-(
I'd like to parse over a file reading line per line.
I want to write everything through a special filter
which exchanges known tokens. This may be special
dictionaries or global variables, too.
The tokens have to be surrounded by '%'-charcter.
Getting a problem with the global varaibles. I couldn't
get their values as string and havn't found the help
in books yet. I think it's quite simple and I'm just a
"four blind mice".;-)
To check the filter I wrote:
-------------
import string, os, re
HEIN="Blöd"
def egalizeTokens(aString):
"changes known tokens in a string"
try:
splitter=re.split('(%.*?%)',aString)
except:
return aString
for s in splitter:
print s
if len(splitter)==1:
aTok = checkForToken(s)
else:
ox = string.split(s, '%')
if len(ox)==1:
aTok = ox[0]
else:
aTok = checkForToken(ox[1])
if aTok==None:
splitter[splitter.index(s)] = s
else:
splitter[splitter.index(s)] = aTok
try:
aTok = string.joinfields(splitter,'')
except:
aTok = splitter[0] #only one element!
return aTok
def checkForToken(aString):
"checks a string for known tokens"
try:
aTok = eval(aString)
if aTok==float(aString): #don't exchange numbers!
return None
return aTok
except:
aTok = os.environ.get(aString)
if aTok==None:
aTok = directories.get(aString)
if aTok==None:
aTok = files.get(aString)
return aTok
if __name__=='__main__':
print egalizeTokens("This is a test! %WINDIR% %1 %2 %HEIN%. That's it")
raw_input ("Press RETURN!")
-------------------------------
Perhaps there's a much easier way to get what I want????
(Still I am a newbie..;-)))
many thanx,
Holger
More information about the Python-list
mailing list