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
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
data.append(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 u'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
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)
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"))
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!
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.