0

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)

1
  • 1
    In the future, check out the json module. In particular, its json.dumps takes a dict and returns a string that can be parsed by JSON.parse on the NodeJS side. This is safer and more readable than string formatting. Commented Dec 15, 2020 at 1:58

3 Answers 3

3

{ and } is the key characters in a string template, you have to escape it with {{ and }}, Try

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)))

You can find more information here: https://docs.python.org/3/library/string.html#format-string-syntax

Sign up to request clarification or add additional context in comments.

Comments

0

If you use Python 3.x You can use f-string for fomatting, I think it's more readable

import sys;

class Color:
  def __init__(self, rgb, brightness):
    self.color = rgb
    self.brightness = brightness;

result_color = Color([11,22,33], 100)    

def show_color(color, brightness):
  [r,g,b] = color;
  sys.stdout.write(f'r: {r}, g: {g}, b: {b}, brightness: {brightness}\n')
  sys.stdout.flush()

show_color(result_color.color, result_color.brightness)

Comments

0

Here's an expanded version of my comment. It uses the builtin json module. I've also shortened the code so it looks neater and doesn't repeat as much.

import sys
import json

...

r, g, b = map(int, result_color.color)
data = dict(r=r, g=g, b=b)

sys.stdout.write(json.dumps(data))
sys.stdout.flush()

Also, I suggest that you use the normal print function. If you need to not have a newline at the end and you want to flush it, both are parameters you can specify.

print(json.dumps(data), end="", flush=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.