Python Tutorial
In python programming, sub function in re python module is used to replace sub string in a string using regular expression with different string.
import re text = "testing in progress" text = re.sub("\s+", " ", text) print(text)
This code replaces all the occurrence of more than one consecutive spaces with one space in the text.
Output:]$ python replace.py testing in progress
import re text = "testing in progress" text = re.sub("[te]+", "@", text) print(text)
This python code is used to replace the one or more occurrence of character "t", "e" or combined string with "@" character in the string.
]$ python replace.py @s@ing in progr@ss
This python program reads the "/proc/bus/pci/devices" file and identifies pci addresses.
#!/usr/bin/env python import re def get_all_pci_addresses(): pci_addresses = [] with open("/proc/bus/pci/devices") as pciinfo: for line in pciinfo: line = re.sub("\s+", " ", line.strip()) fields = line.split(" ") if len(fields) == 18: pci_embed = fields[0] bus = pci_embed[0] + pci_embed[1] dev = str(hex((int(pci_embed[2], 16) << 1) + (int(pci_embed[3], 16) >> 3)))[2:].rjust(2, "0").upper() fun =str(hex(int(pci_embed[3], 16) & 7))[2:].upper() pci = "0000:" + bus + ":" + dev + "." + fun pci_addresses.append(pci) return pci_addresses pci_addresses = get_all_pci_addresses() print(pci_addresses)Output:
$ python pci_addresses.py ['0000:00:02.0', '0000:00:03.0', '0000:00:14.0', '0000:00:16.0', '0000:00:16.3', '0000:00:19.0', '0000:00:1A.0', '0000:00:1B.0', '0000:00:1C.0', '0000:00:1C.1', '0000:00:1D.0', '0000:00:1F.0', '0000:00:1F.2', '0000:00:1F.3', '0000:02:00.0', '0000:03:00.0']
Python Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page