如何使用Python将自定义文件解析为JSON格式
我正在尝试将机器/软件生成的文件类型解析到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下Python模块应该有所帮助。请参阅示例:
请参阅输出为:
The following python module should help. Please see the example:
See the output as: