Reputation: 363
I am completely new to python.
I am using the following code to draw data from a USB device, which is printing data to my raspberry Pi using printf(). I am using the following python code to read this data and print it to screen:
#!/usr/bin/python
import serial
ser = serial.Serial(
port='/dev/ttyUSB0',\
baudrate=115200,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0)
print("connected to: " + ser.portstr)
ser.write("help\n");
while True:
line = ser.readline();
if line:
print(line),
ser.close()
the code prints the following result as is expected (this is what I'm using printf() for):
Received Ticks are: 380 and nodeID is: 1
How can I parse the line variable so that I can save the number of Ticks (380) and nodeID (1), to two variables so that I can use those variables for a HTTP POST request in python?
Upvotes: 1
Views: 11979
Reputation: 4493
split the string, then take the parts you want:
>>> s = "Received Ticks are: 380 and nodeID is: 1"
>>> s.split()
['Received', 'Ticks', 'are:', '380', 'and', 'nodeID', 'is:', '1']
>>> words = s.split()
>>> words[3]
'380'
>>> words[7]
'1'
>>>
Upvotes: 2