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
#1
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: .*"])
    print(variable)
    child.expect(prompt)
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.

Update:

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]*)) 
Reply
#2
child.sendline('some command that lists details')
child.expect(prompt)
output = child.before.decode()
what_you_want = [i.split(':')[1].strip() for i in output.split('\n') if 'Material' in i][0]



#above code should suffice.
I know it is too late, but hope it can still be useful for you elsewhere. Wink
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading in of line not working? garynewport 2 831 Sep-19-2023, 02:22 PM
Last Post: snippsat
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 573 Aug-22-2023, 10:14 AM
Last Post: GYKR
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,088 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Using one child class method in another child class garynewport 5 1,564 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Use pexpect to send user input alisha17 0 1,888 May-10-2022, 02:44 AM
Last Post: alisha17
  Not able to read the text using pexpect/expect Bipinjohnson 7 4,026 Jan-10-2022, 11:21 AM
Last Post: Bipinjohnson
  Sudden Problem with pexpect gw1500se 3 2,382 Nov-19-2021, 11:21 PM
Last Post: bowlofred
  How to use pexpect in python? tiho_bg 1 1,525 Oct-30-2021, 02:50 PM
Last Post: Yoriz
  How to do next line output from CSV column? atomxkai 2 2,079 Oct-02-2021, 01:00 AM
Last Post: Pedroski55
  [Solved] Reading every nth line into a column from txt file Laplace12 7 5,215 Jun-29-2021, 09:17 AM
Last Post: Laplace12

Forum Jump:

User Panel Messages

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