I am reading serial data that is the Key for a Python dictionary. When I read the serial data, it comes through as a byte stream which I convert to a string. When I use that string to search for a dictionary Key, I get a KeyError.
I have tried to search for the same Key by entering the Key manually, and that would perfectly returning the Value. My code is as follows, any help is appreciated!
plates_id = {'TestCylinder100umHard': 8, 'TestCube100umHard': 7, 'MotorMount100umHard': 6, 'InnerShaft100umHard': 5, 'DriveSleeve100umHard': 4, 'DrivePulley100umHard': 3, 'BearingSleeve100umHard': 2, 'Quad': 1, 'Auto Calibration Plate': 0}
s_bytes_new = ser.inWaiting()
s_new = str(ser.read(s_bytes_new))
printing_plate = plates_id[s_new]
print(printing_plate)
Which gives this error:
Traceback (most recent call last):
File "C:/pythonProjects/nanoDLP/touchInterface/printTest.py", line 66, in <module>
printing_plate = plates_id[s_new]
KeyError: "'Auto Calibration Plate'"
Let me know if you need more info. Thanks,
Dylan
Use .decode() instead of str, to convert of bytes to str
s_new = ser.read(s_bytes_new).decode()