I've done my best to recover the code formatting on your original post; see this thread for more instructions on how to do it.
Step 1, most likely, is to drop all this stuff you're trying to do via scripting. Strings Ignition returns to you are natively unicode and you don't have to mess around with encodings in most scenarios.
Post the exact value of what raw_string is as soon as you get it - e.g. use system.util.getLogger("stringDebug").infof("raw string: %s", repr(raw_string))
Also, this is just a hunch, but be very careful with the output of LLMs/AI with regard to Ignition. LLMs need very diligent prompting because they don't fully understand Ignition's context and will happily provide you lots and lots of code you don't actually need.