-1

I'm using fabric to connect to remote linux servers to run commands. I then split it into a list but unable to extract specific data. How can I extract specific data?

Command to get the data:

>>> bpimage_cmd = ssh_connect.run(rf"sudo /usr/openv/netbackup/bin/admincmd/bpimagelist -L -backupid {last_backup_image} -media | egrep  -w 'ID:'")
>>> bpimage_cmd.stdout
'Backup ID:         hostname_1552094084\nJob ID:            4083686\n ID:               L02266\n' 

Split string to list:

>>> bpimage_list = [idx.strip() for idx in bpimage_cmd.stdout.split('\n') if idx.strip()]
>>> bpimage_list
['Backup ID:         hostname_1552094084',
 'Job ID:            4083686',
 'ID:               L02266']

I've tried converting the list to a dict, tried using ast and index to search the list in various forms with out success.

I would like to extract the "value", for example ID:, I would like to get L02266 and store it.

Rohitashwa Nigam
  • 399
  • 5
  • 16
neoluu
  • 37
  • 1
  • 6

2 Answers2

2

How about converting like this ?

    dic = {}
    for element in lis:
        key = element.split(':')[0].strip()
        value = element.split(':')[1].strip()
        dic[key] = value
Rohitashwa Nigam
  • 399
  • 5
  • 16
0

If you use the re module, you can convert the string directly to a dictionary like so:

dic = dict(re.split(r":\s*", s.strip(), 1) for s in bpimage_cmd.stdout.split("\n") if s.strip())

See this question: Splitting a semicolon-separated string to a dictionary, in Python