【问题标题】:Convert text file into json format or filter out key and value pair将文本文件转换为 json 格式或过滤掉键值对
【发布时间】:2017-07-10 13:03:09
【问题描述】:

我尝试执行一项可以从 txt 文件中找出键和值的任务。 我心里有一些想法。 1.使用正则表达式 2.将txt转成json。

我在过滤和转换格式时遇到了困难。有什么好的方法可以做到这一点,或者任何可用的 api 都可以执行这样的任务吗?

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)

提前致谢

【问题讨论】:

  • 这真的是文件的格式,还是 StackOverflow 吃了一些换行符?
  • @Bemmu 我刚刚更新了 txt 格式。这就是我要转换的文件

标签: python json regex


【解决方案1】:

如果文件格式是固定的并且没有嵌套的字典,那么这可以很容易地转换为字典使用

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)[\s\S])*)(?=\s+[^:\n]+:|$)

查看演示。

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

【讨论】:

  • 这几乎是我所需要的,但它的输出与你的不同。我刚刚更新了我上面的txt文件格式。
  • 非常感谢。这正是我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
  • 2019-12-06
  • 2016-06-08
  • 2014-06-26
  • 2017-10-10
  • 1970-01-01
相关资源
最近更新 更多