I'm having difficulty with different string arrays. Previously, there were string arrays only in properties files. Currently, the system has string arrays in properties files and set as environmental variables in the user's .bashrc file. The string arrays look like the following in both the properties and .bashrc files.
STRING_ARRAY="host1","host2","host3"
Previously, there was a simple pair of for loops that read a series of these string arrays and passed them into some function.
for k in ("STRING_ARRAY","SOME_OTHER_ARRAY"):
globals()[k] = globals()[k].replace("\"",'').split(",")
for stringarray,otherarray in zip(STRING_ARRAY, SOME_OTHER_ARRAY):
someFunction(stringarray,otherarray)
This worked fine. The problem arose when some of the variables were moved out of properties files that were passed into the python script and into environmental variables. It seems that when using either os.getenv("HOSTSTRINGARRAY") or os.environ["HOSTSTRINGARRAY"], the os library returns the array of strings without the accompanying quotation marks so
PROPERTIES_STRING_ARRAY="host1","host2","host3"
print PROPERTIES_STRING_ARRAY
returns
"host1","host2","host3"
whereas
ENV_VAR_STRING_ARRAY="host1","host2","host3"
print os.getenv("ENV_VAR_STRING_ARRAY")
returns
host1,host2,host3
This is a problem because I can't seem to mix and match the two types of variables as follows
for k in ("POPERTIES_STRING_ARRAY",os.getenv("ENV_VAR_OTHER_ARRAY")):
globals()[k] = globals()[k].replace("\"",'').split(",")
for stringarray,otherarray in zip(STRING_ARRAY, os.getenv("ENV_VAR_OTHER_ARRAY")):
someFunction(stringarray,otherarray)
So my question is, how do get os.getenv or os.environ to return a comma separated list of strings without stripping off the quotations marks enclosing the individual strings?
Use '
single quote to declare the string. It should work now.
ENV_VAR_STRING_ARRAY='"host1","host2","host3"'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.