1

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?

1
  • regex or simple string splitting Commented Apr 10, 2016 at 13:21

1 Answer 1

2

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'
>>> 
Sign up to request clarification or add additional context in comments.

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.