I'd like to parse the serial data I received on Python and save it as a list.

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

Hello, everyone I'd like to parse the cereal I received in Arduino and save it as a list.

This is my code, and the data is printed in real time like this. I'd like to save the characters in front of : as a list x and the characters in the back as a list y. Can I know how?

python serial-number

2022-09-20 15:00

2 Answers

You can split each into : and zip the list of pairs.

>>> random.choice(range(774, 779))
777
>>> data = [ f"{i}:{random.choice(range(774, 779))}" for i in range(1, 11) ]
>>> data
['1:774', '2:778', '3:775', '4:777', '5:776', '6:777', '7:775', '8:774', '9:776', '10:778']
>>> x, y = zip(*[ e.split(":") for e in data ])
>>> x
('1', '2', '3', '4', '5', '6', '7', '8', '9', '10')
>>> y
('774', '778', '775', '777', '776', '777', '775', '774', '776', '778')
>>> data
['1:774', '2:778', '3:775', '4:777', '5:776', '6:777', '7:775', '8:774', '9:776', '10:778']


2022-09-20 15:00

x = []
y = []

a = "1:111\n" #var = ser.readline()
x.append(a[:-1].split(':')[0])
y.append(a[:-1].split(':')[1])

print(x) #1
print(y) #111


2022-09-20 15:00

If you have any answers or tips


© 2022 pinfo. All rights reserved.