I'm trying to print a string via stdout in Python which will then be picked up by through a child process in NodeJS. My code is as follows:
sys.stdout.write('{r: {0}, g: {1}, b: {2}, brightness: {3}}'.format(int(result_color.color[0]), int(result_color.color[1]), int(result_color.color[2]), int(current_brightness)))
sys.stdout.flush()
but for some reason I get the following error:
Traceback (most recent call last):
File "C:\..\script.py", line 811, in <module>
main(sys.argv[1:])
File "C:\..\script.py", line 756, in main
sys.stdout.write('{r: {0}, g: {1}, b: {2}, brightness: {3}}'.format(int(result_color.color[0]), int(result_color.color[1]), int(result_color.color[2]), int(current_brightness)))
KeyError: 'r'
I can surround the keys with a " ' " but then it is not in the correct format to use JSON.parse() through JS. Why is it exhibiting this behavior? It can't think of a reason for it to be trying to process it as a dictionary when I'm passing print a string. (I want to process this as a string through Python and an Object once received in JS. A dict shouldn't be involved on Python's endd)
json.dumpstakes a dict and returns a string that can be parsed byJSON.parseon the NodeJS side. This is safer and more readable than string formatting.