将文本文件转换为JSON格式或过滤键和值对



我尝试执行一个可以从txt文件中找出键和值的任务。我脑海中有一些想法。1.使用正则2.将txt转到json。

我很难过滤和转换格式。有什么好方法可以执行此类任务吗?

txt文件:

System:    Host: ict-vm Kernel: 4.4.0-53-generic i686 (32 bit)
           Desktop: MATE 1.16.1  Distro: Linux Mint 18.1 Serena
Machine:   System: ASUSTeK (portable) product: N43SN v: 1.0
           Mobo: ASUSTeK model: N43SN v: 1.0 serial: NB-1234567890
           Bios: American Megatrends v: N43SN.412 date: 09/21/2011
CPU:       Quad core Intel Core i7-2670QM (-HT-MCP-) cache: 6144 KB 
           clock speeds: max: 3100 MHz 1: 807 MHz 2: 814 MHz 3: 800 MHz
           4: 1078 MHz 5: 990 MHz 6: 811 MHz 7: 801 MHz 8: 844 MHz
Graphics:  Card-1: Intel 2nd Generation Core Processor Family Integrated Graphics Controller
           Card-2: NVIDIA GF108M [GeForce GT 550M]
           Display Server: X.org 1.18.4 drivers: intel (unloaded: fbdev,vesa) FAILED: nouveau
           tty size: 80x24 Advanced Data: N/A for root
Audio:     Card Intel 6 Series/C200 Series Family High Definition Audio Controller
           driver: snd_hda_intel
           Sound: Advanced Linux Sound Architecture v: k4.4.0-53-generic
Network:   Card-1: Qualcomm Atheros AR9285 Wireless Network Adapter (PCI-Express)
           driver: ath9k
           IF: wlp3s0 state: up mac: 7x:2t:61:d4:72:8a
           Card-2: Qualcomm Atheros AR8151 v2.0 Gigabit Ethernet driver: atl1c
           IF: enp4s0 state: down mac: 14:da:e1:ay:72:b5
Drives:    HDD Total Size: 507.9GB (0.4% used)
           ID-1: /dev/sda model: ST95005620AS size: 500.1GB
           ID-2: USB /dev/sdb model: DataTraveler_3.0 size: 7.8GB
RAID:      No RAID devices: /proc/mdstat, md_mod kernel module present
Sensors:   System Temperatures: cpu: 48.0C mobo: N/A gpu: 43.0
           Fan Speeds (in rpm): cpu: N/A
Info:      Processes: 223 Uptime: 40 min Memory: 531.1/3948.3MB
           Client: Shell (sudo) inxi: 2.2.35System:    Host: ict-vm Kernel: 4.4.0-53-generic i686 (32 bit)
           Desktop: MATE 1.16.1  Distro: Linux Mint 18.1 Serena
Machine:   System: ASUSTeK (portable) product: N43SN v: 1.0
           Mobo: ASUSTeK model: N43SN v: 1.0 serial: NB-1234567890
           Bios: American Megatrends v: N43SN.412 date: 09/21/2011
CPU:       Quad core Intel Core i7-2670QM (-HT-MCP-) cache: 6144 KB 
           clock speeds: max: 3100 MHz 1: 807 MHz 2: 814 MHz 3: 800 MHz
           4: 1078 MHz 5: 990 MHz 6: 811 MHz 7: 801 MHz 8: 844 MHz
Graphics:  Card-1: Intel 2nd Generation Core Processor Family Integrated Graphics Controller
           Card-2: NVIDIA GF108M [GeForce GT 550M]
           Display Server: X.org 1.18.4 drivers: intel (unloaded: fbdev,vesa) FAILED: nouveau
           tty size: 80x24 Advanced Data: N/A for root
Audio:     Card Intel 6 Series/C200 Series Family High Definition Audio Controller
           driver: snd_hda_intel
           Sound: Advanced Linux Sound Architecture v: k4.4.0-53-generic
Network:   Card-1: Qualcomm Atheros AR9285 Wireless Network Adapter (PCI-Express)
           driver: ath9k
           IF: wlp3s0 state: up mac: 7x:2t:61:d4:72:8a
           Card-2: Qualcomm Atheros AR8151 v2.0 Gigabit Ethernet driver: atl1c
           IF: enp4s0 state: down mac: 14:da:e1:ay:72:b5
Drives:    HDD Total Size: 507.9GB (0.4% used)
           ID-1: /dev/sda model: ST95005620AS size: 500.1GB
           ID-2: USB /dev/sdb model: DataTraveler_3.0 size: 7.8GB
RAID:      No RAID devices: /proc/mdstat, md_mod kernel module present
Sensors:   System Temperatures: cpu: 48.0C mobo: N/A gpu: 43.0
           Fan Speeds (in rpm): cpu: N/A
Info:      Processes: 223 Uptime: 40 min Memory: 531.1/3948.3MB
           Client: Shell (sudo) inxi: 2.2.35

json文件:

{
   System: {
      Host:'ict-vm',
      Kernel:'4.4.0-53-generic i686 (32 bit)',
       .....
      },
   Machine:{
      System: 'ASUSTeK (portable)'
    }
}

预期结果:我可以使用键

获得值
print(node['System'])

输出:

ASUSTeK (portable)

预先感谢

如果固定文件格式并且没有嵌套的命令,则可以轻松地将其转动到使用

import re
x="""System:       Host: ict-vm Kernel: 4.4.0-53-generic i686 (32 bit) Desktop: MATE 1.16.1  Distro: Linux Mint 18.1 Serena
Machine:      System: ASUSTeK (portable) product: N43SN v: 1.0 Mobo: ASUSTeK model: N43SN v: 1.0 serial: NB-1234567890 Bios: American Megatrends v: N43SN.412 date: 09/21/2011"""
y = re.findall(r"(S+):s*(.*?)(?=s*S+:|$)",x)
d={}
for i,j in y:
    if not j:
        d[i]={}
        k=d[i]
    else:
        k[i]=j
print d

输出:

{'Machine': {'product': 'N43SN', 'Mobo': 'ASUSTeK', 'System': 'ASUSTeK (portable)', 'Bios': 'American Megatrends', 'v': 'N43SN.412', 'date': '09/21/2011', 'model': 'N43SN', 'serial': 'NB-1234567890'}, 'System': {'Kernel': '4.4.0-53-generic i686 (32 bit)', 'Host': 'ict-vm', 'Distro': 'Linux Mint 18.1 Serena', 'Desktop': 'MATE 1.16.1'}}

您可以使用d['Machine']['System']

访问

输出:ASUSTeK (portable)

编辑:

对于新的输入文件,我们需要调整正则条件。

s*([^:n]+):s*((?:(?!: |n)[sS])*)(?=s+[^:n]+:|$)

请参阅演示。

https://regex101.com/r/ecicpj/1

最新更新