Python: Convert Key/Value Pairs to List of Dictionaries
In my use-case I wanted to store the output of the Linux lsblk command in a list of dictionaries.
The output of lsblk looks something like this:
# lsblk -P -o name,size,type,mountpoint
NAME="sdd" SIZE="250G" TYPE="disk" MOUNTPOINT=""
NAME="sdb" SIZE="50G" TYPE="disk" MOUNTPOINT=""
NAME="sdb2" SIZE="8G" TYPE="part" MOUNTPOINT="[SWAP]"
NAME="sdb3" SIZE="38.4G" TYPE="part" MOUNTPOINT="/"
NAME="sdb1" SIZE="200M" TYPE="part" MOUNTPOINT="/boot/efi"
Note that I use -P to print key/value pairs and -o to limit the number of colums.
Now want to dump this output into a list of dictionaries. I won’t go through the details on how to capture the command line output, so for this example, I’ll simply put the output into a text file named lsblk.out, from where I can easily read it with Python.
The code below opens the file, and iterates over the lines. I use a regular expression to break out the various keys (NAME,SIZE,TYPE,MOUNTPOINT). The re.search uses the regex pattern an puts the results into groups, which I can easily parse and transform into a dictionary.
import re
from pprint import pprint as pp
pattern = r'NAME="(\w*)" SIZE="(\d{0,}.\d{0,}[G|M|K])" TYPE="(\w*)" MOUNTPOINT="(.*)"'
lsblk = []
with open('lsblk.out','r') as reader:
for line in reader.readlines():
found = re.search(pattern,line).groups()
lsblk.append({"name":found[0],"size":found[1],"type":found[2],"mountpoint":found[3]})
pp(lsblk) #Pretty print the list of dictionaries
main_device = list(filter(lambda x: x["mountpoint"] == "/", lsblk))[0]
print(main_device["name"]) #Print the device name that is mounted to /
The secret sauce lies in the regex pattern. The pattern looks for the keys (NAME,SIZE,TYPE,MOUNTPOINT) and spits out the values into a list via the re.search().groups() method.
This method came in handy since I needed to find the devices for different mountpoints. The main_device variable holds the device with mountpoint “/”, by using the filter method on the list.