Make sure to make your thread title more descriptive than “Little scripting help.” This title does nothing to help someone else during a forum search or anyone that may answer your question.
myDict = { 199:"zone A", 200:""}
for key,value in myDict.items():
location = ""
if len(value):
location = "%s Located In: %s" % (str(key),value)
print location
If there’s a variable number of white spaces you might just want to change
if len(value):
to
if len(value.strip()):
Of course it’s probably best to just plan things out so you don’t have to do .strip(). If its truly nothing, you might want to consider using None instead of an empty string but I don’t know your full details.
If you end up going with None instead of ‘’, you could then do
The additional nicety is you can unpack a dictionary inside the format as well though I think this only came with jython 2.7. But its nicer than the old % style of string formatting as you don’t have to manually say what each type is
dataToPrint = {
'intField':1,
'strField':"hello",
"lastField": None
}
f = "int field is {intField} strField is {strField} lastField is {lastField}".format(**dataToPrint)
print f
#prints out "int field is 1 strField is hello lastField is None"
To do similar in Ignition 7.* which uses jython 2.5 you can’t do the .format but you can do
dataToPrint = {
'intField':1,
'strField':"hello",
"lastField": None
}
f = "int field is %(intField)i strField is %(strField)s lastField is %(lastField)s"%(dataToPrint)
print f
# prints out "int field is 1 strField is hello lastField is None"
Exactly, what I’m saying is you don’t have to specify this is an %i, this one is a %d, this one is %s because it implicitly is doing the %s for you. So its can be convenient.
Using ‘’ will make many/most programming languages accept whats inside the ‘’ as litteral text/string and it will not try translate each character in between as a possible “other command”, while using “” the interpeter will look to see if you have like escape characters like /r/n or RegExp expression in it.
This is my main take on the difference.