Python Forum
Reading and storing a line of output from pexpect child
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading and storing a line of output from pexpect child
I'm new to learning pexpect and regex and I'm having some trouble figuring out how to get the output of a command I send and store it in a variable, but for the entire output of that command - I only want to store the rest of 1 specific line after a certain word.

To illustrate - say I have a command that outputs hundreds of lines that all represent certain details of a specific product.

    Color: Maroon Red
    Height: 187cm
    Number Of Seats: 6
    Number Of Wheels: 4
    Material: Aluminum
    Brand: Toyota 
    #and hundreds of more lines...
I want to parse the entire output of the command that I sent which print the details above and only store the material of the product in a variable.

Right now I have something like:

    child.sendline('some command that lists details')
    variable = child.expect(["Material: .*"])
The sendline and expect prompt parts list the details correctly and all, but I'm having trouble figuring out how to parse the output of that command, look for a part that says "Material: " and only store the Aluminum string in a variable.

Right now from playing around I have the variable equal to the value 0 which I guess would be used to verify if a string exists in the output, but I want to access the actual output as a variable and ignore everything after finding a certain string and only include what's after that string on the line for which the the string is located on.

So instead of having variable equal to and print a value of 0, it should instead print the word "Aluminum".

Is there a way to do this using regex? I'm trying to get used to using regex expressions so I would prefer a solution using that but if not, I'd still appreciate any help!

I'm also editing my code in vim and using linux if that helps.


I found something online like:

[\n\r].*Object Name:\s*([^\n\r]*)
which would get the stuff after Object Name: but I wasn't sure how I would store this in my code. Would I do something like:

variable = exp.expect([\n\r].*Object Name:\s*([^\n\r]*)) 

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Solved] Reading every nth line into a column from txt file Laplace12 7 778 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  Pexpect timesout before executing whole output eagerissac 0 239 Jun-23-2021, 03:30 AM
Last Post: eagerissac
  pexpect startup help korenron 2 1,000 Apr-27-2021, 07:23 AM
Last Post: korenron
  How to input & output parameters from command line argument shantanu97 1 706 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Problem with pexpect.exception.TimeOUT korenron 0 460 Apr-12-2021, 03:25 PM
Last Post: korenron
  csv to Json. dynamic child Sureshonly 5 756 Nov-02-2020, 10:20 AM
Last Post: buran
  Request help on pexpect rsurathu 4 1,352 Jul-19-2020, 03:35 PM
Last Post: snippsat
  SIGWINCH ignored when trying to start a screen with the pexpect module StatTark 0 588 Jul-06-2020, 10:12 AM
Last Post: StatTark
  Ignore first few letters of a line when reading file. ShakeyPakey 16 2,239 May-30-2020, 02:17 PM
Last Post: BitPythoner
  can I use 2 child.expect at the same time? korenron 0 598 May-25-2020, 10:41 AM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020