如何在python中将包含多个部分的段落转换为json?

如何在python中将包含多个部分的段落转换为json?,python,python-3.x,Python,Python 3.x,我正在尝试将以下文本转换为json。我没有找到一种方法根据不同的部分来分割它,因为在安装的特性值中有多行。请让我知道实现这一目标的方法?我尝试使用正则表达式,但它不起作用 VER0010I: Copyright (c) IBM Corporation 2002, 2012; All rights reserved. WVER0012I: VersionInfo reporter version 1.15.1.48, dated 2/8/12 --------------------------

我正在尝试将以下文本转换为json。我没有找到一种方法根据不同的部分来分割它,因为在安装的特性值中有多行。请让我知道实现这一目标的方法?我尝试使用正则表达式,但它不起作用

VER0010I: Copyright (c) IBM Corporation 2002, 2012; All rights reserved.
WVER0012I: VersionInfo reporter version 1.15.1.48, dated 2/8/12

--------------------------------------------------------------------------------
IBM WebSphere Product Installation Status Report
--------------------------------------------------------------------------------

Report at date and time July 8, 2020 8:08:44 AM EDT

Installation
--------------------------------------------------------------------------------
Product Directory        /opt/IBM/WebSphere/AppServer
Version Directory        /opt/IBM/WebSphere/AppServer/properties/version
DTD Directory            /opt/IBM/WebSphere/AppServer/properties/version/dtd
Log Directory            /var/ibm/InstallationManager/logs

Product List
--------------------------------------------------------------------------------
ND                       installed
IBMJAVA8                 installed

Installed Product
--------------------------------------------------------------------------------
Name                  IBM WebSphere Application Server Network Deployment
Version               8.5.5.9
ID                    ND
Build Level           cf091608.05
Build Date            2/25/16
Package               com.ibm.websphere.ND.v85_8.5.5009.20160225_0435
Architecture          x86-64 (64 bit)
Installed Features    IBM 64-bit WebSphere SDK for Java
                      WebSphere Application Server Full Profile
                      EJBDeploy tool for pre-EJB 3.0 modules
                      Embeddable EJB container
                      Stand-alone thin clients and resource adapters

Installed Product
--------------------------------------------------------------------------------
Name                  IBM WebSphere SDK Java Technology Edition (Optional)
Version               8.0.2.10
ID                    IBMJAVA8
Build Level           cf091608.04
Build Date            2/24/16
Package               com.ibm.websphere.IBMJAVA.v80_8.0.2010.20160224_1829
Architecture          x86-64 (64 bit)
Installed Features    IBM WebSphere SDK for Java Technology Edition 8

--------------------------------------------------------------------------------
End Installation Status Report
--------------------------------------------------------------------------------

如果节具有多行值,则可以拆分为行,并且每行到列的长度始终相同,然后可以使用循环将行复制到新列表,并检查第一列是否为空-如果为空,则将第二列添加到前一行

text = '''Name                  IBM WebSphere Application Server Network Deployment
Version               8.5.5.9
ID                    ND
Build Level           cf091608.05
Build Date            2/25/16
Package               com.ibm.websphere.ND.v85_8.5.5009.20160225_0435
Architecture          x86-64 (64 bit)
Installed Features    IBM 64-bit WebSphere SDK for Java
                      WebSphere Application Server Full Profile
                      EJBDeploy tool for pre-EJB 3.0 modules
                      Embeddable EJB container
                      Stand-alone thin clients and resource adapters'''

# split to rows
rows = text.split('\n')

# split to columns
rows = [[x[:22].strip(), x[22:].strip()] for x in rows]

# append to previous row
new_rows = []

for col1, col2 in rows:
    if col1:
        # copy without changes
        new_rows.append([col1, col2])
    else:
        # append to last row
        new_rows[-1][1] += '\n' + col2

# convert to dictionary and convert multiline value to list

data = {}

for col1, col2 in new_rows:
    if '\n' in col2:
        col2 = col2.split('\n')
        
    data[col1] = col2        

import json
print(json.dumps(data, indent=4))
结果:

{
    "Name": "IBM WebSphere Application Server Network Deployment",
    "Version": "8.5.5.9",
    "ID": "ND",
    "Build Level": "cf091608.05",
    "Build Date": "2/25/16",
    "Package": "com.ibm.websphere.ND.v85_8.5.5009.20160225_0435",
    "Architecture": "x86-64 (64 bit)",
    "Installed Features": [
        "IBM 64-bit WebSphere SDK for Java",
        "WebSphere Application Server Full Profile",
        "EJBDeploy tool for pre-EJB 3.0 modules",
        "Embeddable EJB container",
        "Stand-alone thin clients and resource adapters"
    ]
}

下面的脚本将报告作为一个整体进行分析

但是,考虑到它可能会中断,因为我们不知道报告的完整格式规则,所以如果输入不同的内容,脚本可能需要调整

上面的用法生成了这个字典,如果您需要它作为json,您可以在上面简单地应用json.dump:

{'IBM WebSphere Product Installation Status Report': 'Report at date and time July 8, 2020 8:08:44 AM EDT',
 'Installation': {'DTD Directory': '/opt/IBM/WebSphere/AppServer/properties/version/dtd',
                  'Log Directory': '/var/ibm/InstallationManager/logs',
                  'Product Directory': '/opt/IBM/WebSphere/AppServer',
                  'Version Directory': '/opt/IBM/WebSphere/AppServer/properties/version'},
 'Installed Product': [{'Architecture': 'x86-64 (64 bit)',
                        'Build Date': '2/25/16',
                        'Build Level': 'cf091608.05',
                        'ID': 'ND',
                        'Installed Features': ['IBM 64-bit WebSphere SDK for Java',
                                               'WebSphere Application Server Full Profile',
                                               'EJBDeploy tool for pre-EJB 3.0 modules',
                                               'Embeddable EJB container',
                                               'Stand-alone thin clients and resource adapters'],
                        'Name': 'IBM WebSphere Application Server Network Deployment',
                        'Package': 'com.ibm.websphere.ND.v85_8.5.5009.20160225_0435',
                        'Version': '8.5.5.9'},
                       {'Architecture': 'x86-64 (64 bit)',
                        'Build Date': '2/24/16',
                        'Build Level': 'cf091608.04',
                        'ID': 'IBMJAVA8',
                        'Installed Features': 'IBM WebSphere SDK for Java Technology Edition 8',
                        'Name': 'IBM WebSphere SDK Java Technology Edition (Optional)',
                        'Package': 'com.ibm.websphere.IBMJAVA.v80_8.0.2010.20160224_1829',
                        'Version': '8.0.2.10'}],
 'Product List': {'IBMJAVA8': 'installed', 'ND': 'installed'}}

你到底试过什么?为什么不起作用?到目前为止你试过什么?您遇到了哪些错误/问题?更重要的是,预期的输出是什么?如果节不同,则对不同的节使用不同的代码。可能首先拆分为行,每行拆分为两个项目列,然后检查某些行的第一列是否为空,并将其添加到前一行。谢谢!!。。。它帮助我解决了这个问题
CONTENT = ... # The full content of the report
import pprint
pprint.pprint(parse_report(CONTENT))
{'IBM WebSphere Product Installation Status Report': 'Report at date and time July 8, 2020 8:08:44 AM EDT',
 'Installation': {'DTD Directory': '/opt/IBM/WebSphere/AppServer/properties/version/dtd',
                  'Log Directory': '/var/ibm/InstallationManager/logs',
                  'Product Directory': '/opt/IBM/WebSphere/AppServer',
                  'Version Directory': '/opt/IBM/WebSphere/AppServer/properties/version'},
 'Installed Product': [{'Architecture': 'x86-64 (64 bit)',
                        'Build Date': '2/25/16',
                        'Build Level': 'cf091608.05',
                        'ID': 'ND',
                        'Installed Features': ['IBM 64-bit WebSphere SDK for Java',
                                               'WebSphere Application Server Full Profile',
                                               'EJBDeploy tool for pre-EJB 3.0 modules',
                                               'Embeddable EJB container',
                                               'Stand-alone thin clients and resource adapters'],
                        'Name': 'IBM WebSphere Application Server Network Deployment',
                        'Package': 'com.ibm.websphere.ND.v85_8.5.5009.20160225_0435',
                        'Version': '8.5.5.9'},
                       {'Architecture': 'x86-64 (64 bit)',
                        'Build Date': '2/24/16',
                        'Build Level': 'cf091608.04',
                        'ID': 'IBMJAVA8',
                        'Installed Features': 'IBM WebSphere SDK for Java Technology Edition 8',
                        'Name': 'IBM WebSphere SDK Java Technology Edition (Optional)',
                        'Package': 'com.ibm.websphere.IBMJAVA.v80_8.0.2010.20160224_1829',
                        'Version': '8.0.2.10'}],
 'Product List': {'IBMJAVA8': 'installed', 'ND': 'installed'}}