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

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

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

Returns:

[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:

D1
D2
D3
.
.
D9
D10
.
.
D19
D21
base = 'D'
x = range(1,23)
x.remove(20)
y = []
for i in x:
	y.append(base+str(iter(x)))
print y

gives me:

['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>']

You are appending the list instead of i.

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

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

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

@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.

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

what the file looks like:

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

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

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.

1 Like

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.

Getting closer:

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:

"['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:

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:

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']"")

[] 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.

2 Likes

Lord have mercy It works:

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!

Just for fun :slight_smile:

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

My dude!

I want to be there :point_up_2:

I am just not comprehending