如何使用Python将自定义文件解析为JSON格式

发布于 2025-01-28 13:58:53 字数 3741 浏览 2 评论 0原文

我正在尝试将机器/软件生成的文件类型解析到JSON文件类型中,以便使用其他软件和其他Python脚本进行分析。该文件的结构与JSON文件类似,但据我所知,该文件的结构性类似。

该文件看起来与此相似(.bpf Filetype):

PACKET fileName.bpf
    STYLE 502
    last_modified 1651620170 # Tue May 03 19:22:50 2022

    STRUCTURE BuildInfo
        PARAM Version
            Value = 1128
        ENDPARAM
        PARAM build_height
            Units = 1 # Inches
            Value = 0.905512
        ENDPARAM
        PARAM build_time_s
            Value = "3:22:53"
        ENDPARAM
        ... # Parameters continue
    ENDSTRUCTURE #BuildInfo only called once

    STRUCTURE PartInfo
        PARAM BandOffset
            Category_mask = 65537
            GUIName = "Stripe Offset"
            Order = 38
            Type = 3
            Units = 1 # Inches
            ZUnits = 1
            profile_z= 0.000000 profile_value = 0.243307
        ENDPARAM
        PARAM Color
            B = 0.380000
            G = 0.380000
            R = 0.380000
            UseDefault = 0
        ENDPARAM
        ... # Parameters continue
    ENDSTRUCTURE #PartInfo ranges from 1 to however many parts are needed, max ~100
ENDPACKET
checksum 0xa61d

我希望最终产品看起来像这样:

{
    "name": "fileName.bpf",
    "style": "502",
    "last_modified": "",
    "BuildInfo": {
        "Version": "1128",
        "build_height": {
            "Units": "1",
            "Value": "0.905512"
        },
        "build_time_s": "3:22:53",
        ... # parameters continue
    },
    "PartInfo-001": [
        "id": "1" #incremented for each part         
        "BandOffset": {
            "Category_mask": "65537",
            "GUIName": "Stripe Offset",
            "Order": "38",
            "Type": "3",
            "Units": "1",
            "ZUnits": "1",
            "profile_z": "0.000000",
            "profile_value": "0.243307",
        }
        "Color": {
            "B": "0.380000",
            "G": "0.380000",
            "R": "0.380000",
        }
        ... # parameters continue
    ... # PartInfo repeats
    ]
}

该文件超过55,000行,参数太多,无法手动创建字典。我开始编写一个脚本以将一个partinfo的文件的小节解析为python词典,然后保存到json文件中,但是该脚本都没有通过文档运行。

# Python program to convert text
# file to JSON

import json

def main():
    # the file to be converted to
    # json format
    filename = r'samplePartParameters.txt'


    # dictionary where the lines from
    # text will be stored
    partParameters = {}

    paramStart = []
    paramEnd = []

    # creating dictionary
    count = 0

    with open(filename, 'r') as file:
        for currentLine in file.readlines():
            if currentLine[0:4:1] == 'PARAM':
                paramStart.append(count)

            elif currentLine[0:2:1] == 'END':
                paramEnd.append(count)


        content = file.readlines()
        numParam = len(paramEnd)
        for paramNum in range(0, numParam-1, 1):
            paramName = content[paramNum][6:]
            partParameters[paramName] = {}
            for propertyNum in range(paramStart[paramNum]+1, paramEnd[paramNum]-1, 1):
                splitPos = content[paramNum].find("=")
                propertyName = content[paramNum][:,splitPos-1]
                propertyVal = content[paramNum][splitPos+1,:]
                partParameters[paramName][propertyName] = propertyVal



    # creating json file
    # the JSON file is named as test1
    out_file = open("test1.json", "w")
    json.dump(partParameters, out_file, indent = 4, sort_keys = False)
    out_file.close()

if __name__ == "__main__":
    print("Running.")
    main()
    print("Done.")

如果您在我的代码中看到错误,或者您知道一种更简单的方法可以解决这个问题,请告诉我。

谢谢!

I am trying to parse a machine/software generated file type into a JSON file type for easy analysis with other software and other Python scripts. The file is structured similarly to a JSON file, but not automatically convertible as far as I can tell.

The file looks similar to this (.bpf filetype):

PACKET fileName.bpf
    STYLE 502
    last_modified 1651620170 # Tue May 03 19:22:50 2022

    STRUCTURE BuildInfo
        PARAM Version
            Value = 1128
        ENDPARAM
        PARAM build_height
            Units = 1 # Inches
            Value = 0.905512
        ENDPARAM
        PARAM build_time_s
            Value = "3:22:53"
        ENDPARAM
        ... # Parameters continue
    ENDSTRUCTURE #BuildInfo only called once

    STRUCTURE PartInfo
        PARAM BandOffset
            Category_mask = 65537
            GUIName = "Stripe Offset"
            Order = 38
            Type = 3
            Units = 1 # Inches
            ZUnits = 1
            profile_z= 0.000000 profile_value = 0.243307
        ENDPARAM
        PARAM Color
            B = 0.380000
            G = 0.380000
            R = 0.380000
            UseDefault = 0
        ENDPARAM
        ... # Parameters continue
    ENDSTRUCTURE #PartInfo ranges from 1 to however many parts are needed, max ~100
ENDPACKET
checksum 0xa61d

I want the end product to look like this:

{
    "name": "fileName.bpf",
    "style": "502",
    "last_modified": "",
    "BuildInfo": {
        "Version": "1128",
        "build_height": {
            "Units": "1",
            "Value": "0.905512"
        },
        "build_time_s": "3:22:53",
        ... # parameters continue
    },
    "PartInfo-001": [
        "id": "1" #incremented for each part         
        "BandOffset": {
            "Category_mask": "65537",
            "GUIName": "Stripe Offset",
            "Order": "38",
            "Type": "3",
            "Units": "1",
            "ZUnits": "1",
            "profile_z": "0.000000",
            "profile_value": "0.243307",
        }
        "Color": {
            "B": "0.380000",
            "G": "0.380000",
            "R": "0.380000",
        }
        ... # parameters continue
    ... # PartInfo repeats
    ]
}

The file is over 55,000 lines with too many parameters to manually create a dictionary out of them. I started writing a script to parse out just a subsection of the file for one PartInfo into a python dictionary, then save to a JSON file, but the script runs through none of the document.

# Python program to convert text
# file to JSON

import json

def main():
    # the file to be converted to
    # json format
    filename = r'samplePartParameters.txt'


    # dictionary where the lines from
    # text will be stored
    partParameters = {}

    paramStart = []
    paramEnd = []

    # creating dictionary
    count = 0

    with open(filename, 'r') as file:
        for currentLine in file.readlines():
            if currentLine[0:4:1] == 'PARAM':
                paramStart.append(count)

            elif currentLine[0:2:1] == 'END':
                paramEnd.append(count)


        content = file.readlines()
        numParam = len(paramEnd)
        for paramNum in range(0, numParam-1, 1):
            paramName = content[paramNum][6:]
            partParameters[paramName] = {}
            for propertyNum in range(paramStart[paramNum]+1, paramEnd[paramNum]-1, 1):
                splitPos = content[paramNum].find("=")
                propertyName = content[paramNum][:,splitPos-1]
                propertyVal = content[paramNum][splitPos+1,:]
                partParameters[paramName][propertyName] = propertyVal



    # creating json file
    # the JSON file is named as test1
    out_file = open("test1.json", "w")
    json.dump(partParameters, out_file, indent = 4, sort_keys = False)
    out_file.close()

if __name__ == "__main__":
    print("Running.")
    main()
    print("Done.")

Please let me know if you see an error in my code, or if you know of an easier way to go about this.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2025-02-04 13:58:53

以下Python模块应该有所帮助。请参阅示例:

!pip install ttp
from ttp import ttp
import json

data_to_parse = """
PACKET fileName.bpf
    STYLE 502
    last_modified 1651620170 # Tue May 03 19:22:50 2022

    STRUCTURE BuildInfo
        PARAM Version
            Value = 1128
        ENDPARAM
        PARAM build_height
            Units = 1 # Inches
            Value = 0.905512
        ENDPARAM
        PARAM build_time_s
            Value = "3:22:53"
        ENDPARAM
        ... # Parameters continue
    ENDSTRUCTURE #BuildInfo only called once

    STRUCTURE PartInfo-001
        PARAM BandOffset
            Category_mask = 65537
            GUIName = "Stripe_Offset"
            Order = 38
            Type = 3
            Units = 1 # Inches
            ZUnits = 1
            profile_z= 0.000000 profile_value = 0.243307
        ENDPARAM
        PARAM Color
            B = 0.380000
            G = 0.380000
            R = 0.380000
            UseDefault = 0
        ENDPARAM
        ... # Parameters continue
    ENDSTRUCTURE #PartInfo ranges from 1 to however many parts are needed, max ~100
ENDPACKET
checksum 0xa61d

    STRUCTURE PartInfo-002
        PARAM BandOffset
            Category_mask = 65538
            GUIName = "Stripe_Offset"
            Order = 39
            Type = 4
            Units = 2 # Inches
            ZUnits = 1
            profile_z= 0.100000 profile_value = 0.253307
        ENDPARAM
        PARAM Color
            B = 0.390000
            G = 0.390000
            R = 0.390000
            UseDefault = 0
        ENDPARAM
        ... # Parameters continue
    ENDSTRUCTURE #PartInfo ranges from 1 to however many parts are needed, max ~100
ENDPACKET
checksum 0xa61d

"""

ttp_template = """
<group name="MyData">
PACKET {{name}}
    STYLE {{style}}
  <group name="{{INFO}}">
    STRUCTURE {{INFO}}
    <group name="{{INFO_TYPE}}">
        PARAM {{INFO_TYPE}}
            Value = {{Value}}
            Units = {{Units}} {{ignore}} {{ignore}}
    </group>
  </group>
  <group name="{{INFO}}">
    STRUCTURE {{INFO}}-{{id}}
    <group name="{{INFO_TYPE}}">
        PARAM {{INFO_TYPE}}
            Value = {{Value}}
            Units = {{Units}} {{ignore}} {{ignore}}
            Category_mask = {{Category_mask}}
            GUIName = {{GUIName}}
            Order = {{Order}}
            Type = {{Type}}
            ZUnits = {{ZUnits}}
            profile_z= {{profile_z}} profile_value = {{profile_value}}
            B = {{B}}
            G = {{G}}
            R = {{R}}
    </group>
  </group>
</group>
"""

parser = ttp(data=data_to_parse, template=ttp_template)
parser.parse()

# print result in JSON format
results = parser.result(format='json')[0]

# converting str to json. 
result = json.loads(results)

print(results)

请参阅输出为:

[
    {
        "MyData": {
            "BuildInfo": {
                "Version": {
                    "Value": "1128"
                },
                "build_height": {
                    "Units": "1",
                    "Value": "0.905512"
                },
                "build_time_s": {
                    "Value": "\"3:22:53\""
                }
            },
            "PartInfo": [
                {
                    "BandOffset": {
                        "Category_mask": "65537",
                        "GUIName": "\"Stripe_Offset\"",
                        "Order": "38",
                        "Type": "3",
                        "Units": "1",
                        "ZUnits": "1",
                        "profile_value": "0.243307",
                        "profile_z": "0.000000"
                    },
                    "Color": {
                        "B": "0.380000",
                        "G": "0.380000",
                        "R": "0.380000"
                    },
                    "id": "001"
                },
                {
                    "BandOffset": {
                        "Category_mask": "65538",
                        "GUIName": "\"Stripe_Offset\"",
                        "Order": "39",
                        "Type": "4",
                        "Units": "2",
                        "ZUnits": "1",
                        "profile_value": "0.253307",
                        "profile_z": "0.100000"
                    },
                    "Color": {
                        "B": "0.390000",
                        "G": "0.390000",
                        "R": "0.390000"
                    },
                    "id": "002"
                }
            ],
            "name": "fileName.bpf",
            "style": "502"
        }
    }
]

The following python module should help. Please see the example:

!pip install ttp
from ttp import ttp
import json

data_to_parse = """
PACKET fileName.bpf
    STYLE 502
    last_modified 1651620170 # Tue May 03 19:22:50 2022

    STRUCTURE BuildInfo
        PARAM Version
            Value = 1128
        ENDPARAM
        PARAM build_height
            Units = 1 # Inches
            Value = 0.905512
        ENDPARAM
        PARAM build_time_s
            Value = "3:22:53"
        ENDPARAM
        ... # Parameters continue
    ENDSTRUCTURE #BuildInfo only called once

    STRUCTURE PartInfo-001
        PARAM BandOffset
            Category_mask = 65537
            GUIName = "Stripe_Offset"
            Order = 38
            Type = 3
            Units = 1 # Inches
            ZUnits = 1
            profile_z= 0.000000 profile_value = 0.243307
        ENDPARAM
        PARAM Color
            B = 0.380000
            G = 0.380000
            R = 0.380000
            UseDefault = 0
        ENDPARAM
        ... # Parameters continue
    ENDSTRUCTURE #PartInfo ranges from 1 to however many parts are needed, max ~100
ENDPACKET
checksum 0xa61d

    STRUCTURE PartInfo-002
        PARAM BandOffset
            Category_mask = 65538
            GUIName = "Stripe_Offset"
            Order = 39
            Type = 4
            Units = 2 # Inches
            ZUnits = 1
            profile_z= 0.100000 profile_value = 0.253307
        ENDPARAM
        PARAM Color
            B = 0.390000
            G = 0.390000
            R = 0.390000
            UseDefault = 0
        ENDPARAM
        ... # Parameters continue
    ENDSTRUCTURE #PartInfo ranges from 1 to however many parts are needed, max ~100
ENDPACKET
checksum 0xa61d

"""

ttp_template = """
<group name="MyData">
PACKET {{name}}
    STYLE {{style}}
  <group name="{{INFO}}">
    STRUCTURE {{INFO}}
    <group name="{{INFO_TYPE}}">
        PARAM {{INFO_TYPE}}
            Value = {{Value}}
            Units = {{Units}} {{ignore}} {{ignore}}
    </group>
  </group>
  <group name="{{INFO}}">
    STRUCTURE {{INFO}}-{{id}}
    <group name="{{INFO_TYPE}}">
        PARAM {{INFO_TYPE}}
            Value = {{Value}}
            Units = {{Units}} {{ignore}} {{ignore}}
            Category_mask = {{Category_mask}}
            GUIName = {{GUIName}}
            Order = {{Order}}
            Type = {{Type}}
            ZUnits = {{ZUnits}}
            profile_z= {{profile_z}} profile_value = {{profile_value}}
            B = {{B}}
            G = {{G}}
            R = {{R}}
    </group>
  </group>
</group>
"""

parser = ttp(data=data_to_parse, template=ttp_template)
parser.parse()

# print result in JSON format
results = parser.result(format='json')[0]

# converting str to json. 
result = json.loads(results)

print(results)

See the output as:

[
    {
        "MyData": {
            "BuildInfo": {
                "Version": {
                    "Value": "1128"
                },
                "build_height": {
                    "Units": "1",
                    "Value": "0.905512"
                },
                "build_time_s": {
                    "Value": "\"3:22:53\""
                }
            },
            "PartInfo": [
                {
                    "BandOffset": {
                        "Category_mask": "65537",
                        "GUIName": "\"Stripe_Offset\"",
                        "Order": "38",
                        "Type": "3",
                        "Units": "1",
                        "ZUnits": "1",
                        "profile_value": "0.243307",
                        "profile_z": "0.000000"
                    },
                    "Color": {
                        "B": "0.380000",
                        "G": "0.380000",
                        "R": "0.380000"
                    },
                    "id": "001"
                },
                {
                    "BandOffset": {
                        "Category_mask": "65538",
                        "GUIName": "\"Stripe_Offset\"",
                        "Order": "39",
                        "Type": "4",
                        "Units": "2",
                        "ZUnits": "1",
                        "profile_value": "0.253307",
                        "profile_z": "0.100000"
                    },
                    "Color": {
                        "B": "0.390000",
                        "G": "0.390000",
                        "R": "0.390000"
                    },
                    "id": "002"
                }
            ],
            "name": "fileName.bpf",
            "style": "502"
        }
    }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文