# Scripting help: inserting data to a table, none type, dictionary, dynamic paths, f-string

**URL:** <https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853>\
**Category:** Ignition\
**Created:** [December 7, 2021, 7:23pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853 "2021-12-07T19:23:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [December 7, 2021, 7:23pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/1 "2021-12-07T19:23:26Z")

</div>

in my dictionary I have:

```auto
199: "zone A",
200: " ",

```

so in my script, I am trying to say something like

```auto
if value=" "
 value=" "
else
 value="located in "+value

```

How do I ask if value is equal to a space?

update  
I was missing syntax stuff like colons and an additional equals

```auto
if value == " ":
		value=" "
else:
           value = "located in "+value

```

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [December 7, 2021, 7:36pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/2 "2021-12-07T19:36:20Z")

</div>

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.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [December 7, 2021, 7:37pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/3 "2021-12-07T19:37:48Z")

</div>

If there’s a variable number of white spaces you might just want to change

```auto
if len(value):

```

to

```auto
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

```auto
if value is not None:

```

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [December 7, 2021, 7:39pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/4 "2021-12-07T19:39:31Z")

</div>

I noticed that there looked like a space in the value, but I figured that to be a typo. Definitely not a good practice, good to point that out.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [December 7, 2021, 7:40pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/5 "2021-12-07T19:40:55Z")

</div>

Always better to squash a code smell before they turn into a code stench

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [December 7, 2021, 7:56pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/6 "2021-12-07T19:56:48Z")

</div>

I had no idea what to make the title.

what is

```auto
% (str(key),value)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 7, 2021, 8:06pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/7 "2021-12-07T20:06:01Z")

</div>

> [@zacharyw.larson](#):
>
> `% (str(key),value)`

[https://docs.python.org/2.7/library/stdtypes.html#string-formatting-operations](https://docs.python.org/2.7/library/stdtypes.html#string-formatting-operations)

Consider going through some Python tutorials.

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [December 7, 2021, 9:09pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/8 "2021-12-07T21:09:53Z")

</div>

Writing the phrase with the converter and then casting the key as a string is mental gymnastics for me.  
I can see how it would be very useful though.

It was hard for me to understand what the [docs.python.org](http://docs.python.org) was saying.

this was way more my speed

> **[Python Casting](https://www.w3schools.com/python/python_casting.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 7, 2021, 9:20pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/9 "2021-12-07T21:20:45Z")

</div>

Casting the key to string in the formatting operation was actually superfluous, as the `%s` conversion format specifier would do that anyways.

But, anyways, whether via docs or tutorials, learning python’s basics will make Ignition much easier to navigate.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [December 8, 2021, 9:43am UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/10 "2021-12-08T09:43:14Z")

</div>

You can also use

```python
"something {variable} format".format(variable="you want to")

```

Which I think was the recommended syntax before f-strings.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [December 8, 2021, 1:29pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/11 "2021-12-08T13:29:02Z")

</div>

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

```auto
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

```auto
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"

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 8, 2021, 1:46pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/12 "2021-12-08T13:46:38Z")

</div>

> [@bkarabinchak.psi](#):
>
> string formatting as you don’t have to manually say what each type is

No, it is equivalent to using `%s` everywhere. I'm not a fan of the curly braces.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [December 8, 2021, 1:49pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/13 "2021-12-08T13:49:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [December 8, 2021, 1:58pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/14 "2021-12-08T13:58:08Z")

</div>

I find them much more readable.

```python
"{level} error happened in {device} at {time}"

```

vs

```python
"%s error happened in %s at %s"

```

especially when you start applying formats to your variables:

```python
"foo {bar:>10}".format(bar="bar")

```

Actually, I’m not even sure how you do this with `%`…

Besides, specifying a type reminds me a bit too much of `printf`

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [December 16, 2021, 3:50pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/15 "2021-12-16T15:50:24Z")

</div>

What about for dynamic paths?

[Tag Paths - Ignition User Manual 8.1 - Ignition Documentation (inductiveautomation.com)](https://docs.inductiveautomation.com/display/DOC81/Tag+Paths)

```auto
narrator =Clarkson
convoyNumber = PQ17
tagPath = ""[default]%s/%s" % Clarkson,PQ17

```

is that going to set the tagpath as:

```auto
[default]Clarkson/PQ17

```

I think % is throwing an illegal character in script console

I like the curly braces, but I think those only work in expression

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [December 16, 2021, 3:55pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/16 "2021-12-16T15:55:18Z")

</div>

You have a couple of errors in your script. It should be:

```auto
narrator = 'Clarkson'
convoyNumber = 'PQ17'
tagPath = '[default]%s/%s' % (narrator,convoyNumber)

```

This will set the tagPath to `[default]Clarkson/PQ17`

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [December 16, 2021, 4:00pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/17 "2021-12-16T16:00:05Z")

</div>

I did something wrong before, seems to be working in script console.  
Thanks

Why are you using ’ ’ instead of " "

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [December 16, 2021, 4:15pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/18 "2021-12-16T16:15:26Z")

</div>

> [@zacharyw.larson](#):
>
> Why are you using ’ ’ instead of " "

Habit/laziness (don't have to use the shift key) 🤣.

Officially, python (jython by extension) will accept either. This allows you to do things like nested quotes without escape characters.

`'This will allow "quotes" inside of a string'`

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 16, 2021, 4:26pm UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/19 "2021-12-16T16:26:23Z")

</div>

> [@lrose](#):
>
> python (jython by extension) will accept either

Also true for Ignition's expression language. But this is _ **not** _ true for SQL.

---

<div class="post-metadata">

**Author:** ![SKA](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ska/32/29528_2.png) [@SKA](https://forum.inductiveautomation.com/u/SKA)\
**Post date:** [December 17, 2021, 7:14am UTC](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853/20 "2021-12-17T07:14:10Z")

</div>

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.

[Next page](https://forum.inductiveautomation.com/t/scripting-help-inserting-data-to-a-table-none-type-dictionary-dynamic-paths-f-string/53853.md?page=2)
