# How to String Format or append using range list? Edit: Read txt file on mapped drive and do something

**URL:** <https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345>\
**Category:** Ignition\
**Created:** [January 16, 2020, 4:37pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345 "2020-01-16T16:37:41Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [January 16, 2020, 4:37pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/1 "2020-01-16T16:37:41Z")

</div>

```auto
base = 'D'
x = range(1,23)
x.remove(20)
print x

for i in x:
	test = '%s%s' % (base,x)
	print test

```

Returns:

```auto
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]
D[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22]

```

I neeed to work towards an output of:

```auto
D1
D2
D3
.
.
D9
D10
.
.
D19
D21

```

```auto
base = 'D'
x = range(1,23)
x.remove(20)
y = []
for i in x:
	y.append(base+str(iter(x)))
print y

```

gives me:

```auto
['D<listiterator object at 0x53>', 'D<listiterator object at 0x54>', 'D<listiterator object at 0x55>', 'D<listiterator object at 0x56>', 'D<listiterator object at 0x57>', 'D<listiterator object at 0x58>', 'D<listiterator object at 0x59>', 'D<listiterator object at 0x5a>', 'D<listiterator object at 0x5b>', 'D<listiterator object at 0x5c>', 'D<listiterator object at 0x5d>', 'D<listiterator object at 0x5e>', 'D<listiterator object at 0x5f>', 'D<listiterator object at 0x60>', 'D<listiterator object at 0x61>', 'D<listiterator object at 0x62>', 'D<listiterator object at 0x63>', 'D<listiterator object at 0x64>', 'D<listiterator object at 0x65>', 'D<listiterator object at 0x66>', 'D<listiterator object at 0x67>']

```

---

<div class="post-metadata">

**Author:** ![jpark](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jpark](https://forum.inductiveautomation.com/u/jpark)\
**Post date:** [January 16, 2020, 4:41pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/2 "2020-01-16T16:41:43Z")

</div>

You are appending the list instead of i.

```auto
for i in x:
	test = '%s%d' % (base,i)
	print test

```

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [January 16, 2020, 4:45pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/3 "2020-01-16T16:45:02Z")

</div>

> [@jpark](#):
>
> instead of i.

Aahhhhhhh I'm going to get it eventually.... thanks for the help!......again lol

---

<div class="post-metadata">

**Author:** ![jpark](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jpark](https://forum.inductiveautomation.com/u/jpark)\
**Post date:** [January 16, 2020, 4:45pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/4 "2020-01-16T16:45:56Z")

</div>

```auto
base = "D"
for i in [x for x in xrange(1,22) if x !=20]:
	print "%s%d"%(base,i)

```

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [January 16, 2020, 5:02pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/5 "2020-01-16T17:02:17Z")

</div>

@jpark

Dang, you helped me yesterday and today on both parts of this lol

For future readers:

The following is an example of how to navigate to a portion of a mapped file directory read a .txt file and return the latest entry that matches a re pattern. This is useful for CNC logs and whatnot. YMMV this doesn’t have things like error or exception handling like it should. Would have to be implemented in a different way as well… In my case Inside my UDT for these machines I am now using an expression tag with runScript(). Where I have a parameter in the UDT that is the machine name.  
in my case : ‘D01’ Or ‘D20’ note file directories don’t match.

runScript(mes.get\_file\_part({machinename})) :untested

Eventually, I am going to have to figure out how to look in a mapped directory and then copy those files and folders on to the webserver directory of the gateway machine. So, my users, can self serve. The idea would be to place work instructions and blueprints and so on to a mapped drive in a certain schema. And making that available at the machine level through a tree and pdf viewer. I think this lends itself to that. Unless someone can suggest a different methodology.

```auto
import system
import re

def get_file_part(machine):

	if len(machine) == 3 and machine[1] == '0':
		machine = machine[0]+machine[2]
	else: 
		pass
	path = ('Z:\Qa_Reports\Daily Production Data\%s\%s_PCM.txt' % (machine,machine))
	x = system.file.readFileAsString(path)
	result = re.findall('\s\d\d\d\s', x)
	z = result[-1]
	t = z.strip()
	return t

base = 'D'
x = range(1,23)
x.remove(20)
new = []
for i in x:
	test = '%s%d' % (base,i)
	new.append(test)
for i in new:
	c = mes.get_file_part(i)
	print i
	print c

```

```auto
D1
985
D2
262
D3
038
D4
260
D5
320
D6
691
D7
289
D8
289
D9
690
D10
987
D11
081
D12
812
D13
387
D14
648
D15
658
D16
690
D17
086
D18
275
D19
936
D21
881
D22
480

```

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [January 16, 2020, 5:22pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/6 "2020-01-16T17:22:04Z")

</div>

what the file looks like:

```auto
D3 038 060214,01/14/20,11:24,10032959
D3 038 060214,01/14/20,11:30,10033016
D3 038 060214,01/14/20,11:36,10033086
D3 038 060214,01/14/20,11:42,10033143
D3 038 060214,01/14/20,11:48,10033210
D3 038 060214,01/14/20,11:54,10033268
D3 038 060214,01/14/20,12:00,10033337
D3 038 060214,01/14/20,12:12,10033463
D3 038 060214,01/14/20,12:18,10033533
D3 038 060214,01/14/20,12:24,10033590
D3 038 060214,01/14/20,12:30,10033659
D3 038 060214,01/14/20,12:36,10033717

```

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [January 16, 2020, 5:33pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/7 "2020-01-16T17:33:42Z")

</div>

make sure to map drive in java or it will work until it decides not too…

[https://docs.inductiveautomation.com/display/DOC80/Mapping+a+Network+Drive](https://docs.inductiveautomation.com/display/DOC80/Mapping+a+Network+Drive)

---

<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:** [January 16, 2020, 5:45pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/8 "2020-01-16T17:45:44Z")

</div>

You are doing network file access within a runScript() expression tag? You understand that the slightest network hiccup will freeze your scan class/tag group? This is a job for a timer event on a dedicated thread.

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [January 16, 2020, 5:58pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/9 "2020-01-16T17:58:27Z")

</div>

No, I didn’t know that…  
that makes sense though.

Gateway time script seems more logical.

How to do it in a deterministic way though?

hmm…

I did the same thing awhile back and you helped me then. Where I put ‘clean time entries’ in these machines associated SQL tables. Because I didn’t understand how to query and return clean groups with whole DateTime values yet.

So I eventually want to make this where. The file path is a UDT tag or param or whatever don’t care so people can re-map. But, I need to run it from the gateway…

Yikes, for now, I’ll run it in gateway timer script and just do write blocking to where the need to go?

Also, need to figure out what a dedicated thread is lol edit: just check box when setting up gateway timer script.

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [January 16, 2020, 6:57pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/10 "2020-01-16T18:57:29Z")

</div>

Getting closer:

```auto
base = 'D'
x = range(1,23)
x.remove(20)
new = []
ob = []
keys , values = [] , []
for i in x:
	test = '%s%d' % (base,i)
	new.append(test)
for t in new:
	c = mes.get_file_part(t)
	keys.append(t)
	values.append(c)	
test = '[{}]'.format(', '.join("'{}': '{}'".format(k, v) for k, v in zip(keys, values)))
tagdata = system.util.jsonEncode(test)
print tagdata

system.tag.write('[default]SCADA/playground/New Tag 1',tagdata)

```

returns:

```auto
"['D1': '985', 'D2': '262', 'D3': '038', 'D4': '260', 'D5': '320', 'D6': '691', 'D7': '289', 'D8': '289', 'D9': '690', 'D10': '987', 'D11': '081', 'D12': '812', 'D13': '387', 'D14': '648', 'D15': '658', 'D16': '690', 'D17': '086', 'D18': '275', 'D19': '936', 'D21': '881', 'D22': '480']"

```

But using:

```auto
jsonGet({[.]New Tag 1.value},'D1')

```

on an expression tag complains about:  
ive tried document, dataset and string:  
also tried getting rid of leading and trailing quotes and same results:

```auto
Error_ExpressionEval("Error executing jsonGet(): Not a JSON Object: "['D1': '985', 'D2': '262', 'D3': '038', 'D4': '260', 'D5': '320', 'D6': '691', 'D7': '289', 'D8': '289', 'D9': '690', 'D10': '987', 'D11': '081', 'D12': '812', 'D13': '387', 'D14': '648', 'D15': '658', 'D16': '690', 'D17': '086', 'D18': '275', 'D19': '936', 'D21': '881', 'D22': '480']"")

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 16, 2020, 7:11pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/11 "2020-01-16T19:11:46Z")

</div>

`[]` defines a json array, which isn’t a “mapping” type. Try using curly braces, so your string ends up being:  
`{"D1": 982, "D2": 262}`, etc. Also, not sure if it matters for our function(s), but legal JSON has to use double quotes, no single quotes.

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [January 16, 2020, 11:37pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/12 "2020-01-16T23:37:51Z")

</div>

Lord have mercy It works:

```auto
base = 'D'
x = range(1,23)
x.remove(20)
new = []
ob = []
keys , values = [] , []
for i in x:
	test = '%s%d' % (base,i)
	new.append(test)
for t in new:
	c = mes.get_file_part(t)	
	keys.append(t)
	values.append(c)
res = {} 
for key in keys: 
    for value in values: 
        res[key] = value 
        values.remove(value) 
        break  
	
	
print res
system.tag.write('[default]SCADA/playground/New Tag 1',res)

```

I’ll post here in case it could get anyone else started. I am able to write this to a ‘global’ tag of sorts and use a param in the udt as key and json get expression tag inside udt to sort it all out. Should have exception handling and what not added. But, I have other fish to fry and prove out…

Thanks to all!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 16, 2020, 11:47pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/13 "2020-01-16T23:47:47Z")

</div>

Just for fun 🙂

```auto
res = {key: mes.get_file_part(key) for key in ["D%d" % i for i in xrange(1, 23) if i != 20]}
print res

```

---

<div class="post-metadata">

**Author:** ![wking](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wking/32/21823_2.png) [@wking](https://forum.inductiveautomation.com/u/wking)\
**Post date:** [January 16, 2020, 11:59pm UTC](https://forum.inductiveautomation.com/t/how-to-string-format-or-append-using-range-list-edit-read-txt-file-on-mapped-drive-and-do-something/31345/14 "2020-01-16T23:59:24Z")

</div>

My dude!

I want to be there 👆

I am just not comprehending
