【发布时间】:2021-05-25 11:35:48
【问题描述】:
我正在尝试制作一个允许用户将其原始文件动态格式化为 JSON 格式的程序。 目前,我设法使用硬编码的值/索引将文本文件制作为 JSON 格式。但我想要的是让用户创建自己的值并将某些字符串从原始文件中提取为 json 格式。让用户自定义自己的键和值进行转换 有没有办法实现我的目标?可以用 ReGex 完成吗? 场景:用户上传他们的文本文件,他们可以选择要存储的密钥和字符串,然后转换为 json 格式
我要提取的示例文件: 其他用户也可以上传其他文件
13-May-2020 14:19:14;176039082005130240000000 SYNC RotaryRight_1002110 MeasureATI;RotKnob_EMERGENCY-EXIT
13-May-2020 14:19:29;176039082005130240000000 SYNC RotaryRight_1003010 Start Communication_ERROR:Key in Position 'MANUAL'
13-May-2020 14:19:29;176039082005130240000000 SYNC RotaryLeft_1002010 Start Communication_ERROR:Key in Position 'MANUAL'
13-May-2020 14:31:18;176039082005130321000000 SYNC RotaryRight_1002110 MeasureATI;RotKnob_EMERGENCY-EXIT
13-May-2020 14:41:52;176039082005130291000000 SYNC RotaryRight_1002110 MeasureATI;RotKnob_EMERGENCY-EXIT
13-May-2020 14:49:58;176039082005130301000000 SYNC RotaryRight_1002110 MeasureATI;RotKnob_EMERGENCY-EXIT
我的硬编码代码:
for content in data2:
splitted_line = content.split(" ")
print(splitted_line)
file_data = {}
file_data["Date"] = splitted_line[0]
time_and_code = splitted_line[1].split(";")
file_data["Time"] = time_and_code[0]
file_data["Code"] = time_and_code[1]
file_data["Status"] = splitted_line[3]
file_data["Message"] = " ".join(splitted_line[4:])
# output to JSON
global tmp
tmp = json.dumps(file_data, ensure_ascii=False, indent="\t")
reviewjson.insert(END, tmp)
结果
{
'Date': '13-May-2020',
'Time': ' 12:49:35',
'Code': ' 176036072005120136000000'
'Sync Status': 'SYNC'
' Message': ' RotaryRight_1001010 MOV P1 -> Park Position_BUSY'
}
【问题讨论】: