Hi All,
This is a solution to a problem I solved while preparing a post for this forum. Just thought I’d share the ‘riches’.
I was having trouble saving a JSON file that contains non-ASCII characters.
I got material data from okobaudat database API using the Swiftlet plugin (which is great, btw) . I got strings that contained non-ASCII characters and viewed them in the GH canvas perfectly fine.
(Data from swiftlet is internalized):
Printing in the GHPython Editor also appears as desired, and printing through repr() shows the encoding… but which encoding???
A little guess and check showed that the characters before and after encoding matched when I used ‘Latin1’.
`print SwiftletString[0:256]
print '_________________________________'
print repr(SwiftletString[0:256])
print '_________________________________'
convertedString = SwiftletString.encode('Latin1', 'replace')
print convertedString[0:256]
print '_________________________________'
print repr(convertedString[0:256])`
Here’s what prints from those statements:
The second to last step was to convert the string to a dictionary using the ‘Latin1’ encoding (this was a necessary step for me, because I wanted to retrieve data from the JSON file okobaudat provides using ghpython)
convertedDictionary = json.loads(convertedString, encoding='Latin1'
… and then finally write that dictionary out to a JSON file.
filePath = PathToWriteJSON + ".json"
JSONOut = open(filePath, "wb")
json.dump(convertedDictionary, JSONOut, encoding='Latin1', ensure_ascii=False)
JSONOut.close()
OutputPath = filePath