writeFile won't write as string when tag value is included

tags = system.tag.browseTags(tagPath = 'path/to/*/tags', recursive=True, sort='ASC')
filePath = system.file.saveFile('C:\\Users\\username\\Desktop\\tags.txt')
for tag in tags:
	system.file.writeFile(filePath, tag.path+'\n', 1)

That works fine, but I need to include the tags value as well…

tags = system.tag.browseTags(tagPath = 'path/to/tags', recursive=True, sort='ASC')
filePath = system.file.saveFile('C:\\Users\\username\\Desktop\\tags.txt')
for tag in tags:
	tagValue = str(system.tag.read(tag.path).value)
	system.file.writeFile(filePath, tag.path+'\t'+tagValue+'\n', 1)

Then when I open the txt file it is misc characters. I’ve double checked the tag types and they’re all OPC string tags and I’ve tried casting charData as a string like this:
system.file.writeFile(filePath, str(tag.path+'\t'+tagValue+'\n'), 1)
but still no luck.

type(tagValue) says theyre all strings as well

Is there a way to explicitly tell system.file.writeFile to write as a string?

I should probably mention I’m doing this in the scripting Console on 7.9.18


Can you show what you are expecting and what you get? It sounds like it might be a character encoding issue.

I’m expecting this:

path/to/1/tag    value
path/to/2/tag    value
path/to/3/tag    value

(the actual script has a wildcard ‘path/to/*/tags’, I’m not sure why I left that out… I’ll edit my original post)

I’m getting this:

I cannot include specific examples because the outputs have brand names in them, but I’ve been doing more testing and I’m getting strange values from these tags.
The following code prints the values as strings, exactly how they appear in the Tag Browser:

for tag in tags:
	tagValue = str(system.tag.read(tag.path).value)
	print tagValue

This next bit of code produces outputs that vary drastically from value to value:

data = []
for tag in tags:
	tagValue = str(system.tag.read(tag.path).value)
	print tagValue
print data

The print statement in the loop outputs the value I expect, printing the list does not.
Some of the values start with ‘\x00’ instead of the first letter, some of them have a few or every space replaced with ‘\x00’, some have 0s replaced with ‘\x00’, some look as expected.

Try unicode() instead of str(). You’ll also likely have some issues due to 7.9’s use of Jython 2.5; Python 2 in general has rough unicode compatibility, but 8.X (using Jython 2.7) is at least better.

unicode() didn’t work, unfortunately. The txt file looks the same.

One of the tags’ values is just 32 spaces, print tagValue shows 32 white spaces

print data shows this for that same tag value

Let’s back up.

You’re trying to read a series of string tags (with Chinese characters?).
The values are correctly stored on these tags, and visible in the tag browser.
If you print the literal values of these tags in a loop, you get the output you expect.
Printing the values after inserting them into a list doesn’t get you the output you expect, but that’s not actually an indication of a problem, necessarily. Python’s string representation of a list (basically) calls __repr__ on the list items:

data = [

for item in data:
	print item
print data

for item in data:
	print repr(item)


['\xe7\xb6\xb2\xe7\xab\x99\xe6\x9c\x89\xe4\xb8\xad\xe8\x8b\xb1\xe6\x96\x87\xe7\x89\x88\xe6\x9c\xac', '\xe4\xb9\x9f\xe6\x9c\x89\xe7\xb9\x81', '\xe7\xb0\xa1\xe9\xab\x94\xe7\x89\x88', '\xe5\x8f\xaf\xe9\x80\x9a\xe9\x81\x8e\xe6\xaf\x8f\xe9\xa0\x81\xe5\xb7\xa6\xe4\xb8\x8a\xe8\xa7\x92\xe7\x9a\x84\xe9\x80\xa3\xe7\xb5\x90\xe9\x9a\xa8\xe6\x99\x82\xe8\xaa\xbf\xe6\x95\xb4']

What might be the issue is encoding in system.file.writeFile. In later versions, you can specify the encoding of string data as an argument, but you’ll need to go through some extra steps in 7.9 (I also migrated your code to create the string in one go, and to read all your tags in one call, both of which will increase the speed a fair bit):

tags = [tag.path for tag in system.tag.browseTags(tagPath = 'path/to/tags', recursive=True, sort='ASC')]
filePath = system.file.saveFile('C:\\Users\\username\\Desktop\\tags.txt')
values = [qv.value for qv in system.tag.readAll(tags)]

body = "\n".join(
	path + '\t' + value
	for path, value
	in zip(tags, values)

from java.lang import String
system.file.writeFile(filePath, String(body).getBytes("UTF-8"))
1 Like

You’re right, the string literal vs repr accounts for the differences in printing in the shell.
The OPC tags I’m reading are English alphanumeric, through some encoding mix up the txt file was filled with a variety of characters like in the screenshot I posted (these characters are incorrect).
Using the java String class and specifying UTF-8 worked perfect. Thank you!

1 Like

Good to hear. Whenever you’re able to upgrade to an 8.X version, you should able to drop the java.lang.String import and just add the UTF-8 argument to system.file.writeFile directly, but glad it’s working in the meantime.