After executing the query, I get a string of parameters, among which are the following:

name="Bob" job=artist age="35" date=2018.12.21 

How to display the values ​​of the parameters in this form:

 Имя - Bob Работа - artist Возраст - 35 Дата - 2018.12.21 

I try through re.findall with the search for the words name = job =, etc., but I do not get what I need (I can not display the value without quotes and the date is cut off after a year).

I get the string via ssh:

 stdin, stdout, stderr = ssh.exec_command('grep -rw '/root/list.txt' -e 'Bob'') data = stdout.read() + stderr.read() print(data) b'name="Bob" job=artist age="35" town=Chehov date=2018.12.21 active="yes"\n' 
  • one
    How exactly does the string look like, can you give an example? - Andrey
  • one
    At the same time, you can tell in more detail exactly how you get it and where it comes from. - Sergey Gornostaev 2:51 pm

1 answer 1

 data = b'name="Bob" job=artist age="35" date=2018.12.21 active="yes"\n' data = data.decode() values = {k: v for (k, v) in (item.replace('"', '').split('=') for item in data.split()) } print('Имя - {}'.format(values['name'])) print('Работа - {}'.format(values['job'])) print('Возраст - {}'.format(values['age'])) print('Дата - {}'.format(values['date'])) 
  • I would replace print ('Name - {}'. format (values ​​['name'])) with print (f 'Name - {values ​​[' name ']}) - garrythehotdog pm
  • @garrythehotdog, I also really like the syntax of f-lines. But, version 3.6+ is far from everyone’s worth, so I wouldn’t have pushed the syntax from these versions by default :) - Xander
  • @ Alexander, your example works. I have errors: b'name = "Bob" job = artist age = "35" town = Spb date = 2018.12.21 active = "yes" \ n 'Traceback (most recent call last): File "c: \ test. py ", line 11, in <module> (item.replace ('"', ''). split ('=') for item in data.split ()) File "c: \ test.py", line 10 , in <dictcomp> values ​​= {k: v for (k, v) in File "c: \ test.py", line 11, in <genexpr> (item.replace ('"', ''). split ( '=') for item in data.split ()) TypeError: a bytes-like object is required, not 'str' - Ven0m
  • one
    @ Ven0m, you have a byte string. Such things should be indicated in the question immediately. To cast a byte string to normal, you can use the decode method. I corrected my answer, now it should work for you. - Xander