从 .txt 中提取多行到字典
Extracting multiline from .txt to dictionary
我正在尝试找出如何使用 python 编写以下问题的代码。假设我们在 .txt 文件中有以下数据集:
package autoload
config core 'main'
option Enabled 'no'
option StartTimer '120'
option RetryTimer '30'
option BackoffTimer '15'
option BootUsingConfig 'altconfig'
option BootUsingImage 'altimage'
config entry
option Configured 'yes'
option SegmentName 'altconfig'
option RemoteFilename '$$.ini'
package cwatch
config watch '3g_watch'
option enabled 'yes'
option test_ifaces 'wan1 wan2'
option failure_time_1 '30m'
option failure_action_1 'reboot'
期待这样的结果:
{"autoload":{"core 'main'":{"Enabled": "no", "StartTimer": "120", ...},
"entry":{"Configure": "yes", "SegmentName": "altconfig", ...},
...},
"cwatch": {"watch '3g_watch'":{"Enabled": "yes", "test_ifaces":"wan1 wan2", ...}}
}
我被困在这里,不知道下一步该做什么。
numRegex = re.compile(r'^package (\w*)\s*^config (\w*.*\w*)', re.M)
with open(file) as f:
data = {m.groups() for m in numRegex.finditer(f.read())}
这听起来很像 Python configuration file,您有内置的解析器。这能解决问题吗?
在有人使用 regexp-oneliner 到来之前,我会使用老式状态机:
bundle = {}
with open("config.txt") as f:
for line in f:
line=line.strip().split()
if line:
if line[0] == "package":
package = {}
bundle[line[1]] = package
elif line[0] == "config":
config = {}
package[" ".join(line[1:])] = config
elif line[0] == "option":
config[line[1]] = " ".join(line[2:])
print(bundle)
结果是
{'autoload': {"core 'main'": {'Enabled': "'no'", 'StartTimer': "'120'", 'RetryTimer': "'30'", 'BackoffTimer': "'15'", 'BootUsingConfig': "'altconfig'", 'BootUsingImage': "'altimage'"}, 'entry': {'Configured': "'yes'", 'SegmentName': "'altconfig'", 'RemoteFilename': "'$$.ini'"}}, 'cwatch': {"watch '3g_watch'": {'enabled': "'yes'", 'test_ifaces': "'wan1 wan2'", 'failure_time_1': "'30m'", 'failure_action_1': "'reboot'"}}}
我正在尝试找出如何使用 python 编写以下问题的代码。假设我们在 .txt 文件中有以下数据集:
package autoload
config core 'main'
option Enabled 'no'
option StartTimer '120'
option RetryTimer '30'
option BackoffTimer '15'
option BootUsingConfig 'altconfig'
option BootUsingImage 'altimage'
config entry
option Configured 'yes'
option SegmentName 'altconfig'
option RemoteFilename '$$.ini'
package cwatch
config watch '3g_watch'
option enabled 'yes'
option test_ifaces 'wan1 wan2'
option failure_time_1 '30m'
option failure_action_1 'reboot'
期待这样的结果:
{"autoload":{"core 'main'":{"Enabled": "no", "StartTimer": "120", ...},
"entry":{"Configure": "yes", "SegmentName": "altconfig", ...},
...},
"cwatch": {"watch '3g_watch'":{"Enabled": "yes", "test_ifaces":"wan1 wan2", ...}}
}
我被困在这里,不知道下一步该做什么。
numRegex = re.compile(r'^package (\w*)\s*^config (\w*.*\w*)', re.M)
with open(file) as f:
data = {m.groups() for m in numRegex.finditer(f.read())}
这听起来很像 Python configuration file,您有内置的解析器。这能解决问题吗?
在有人使用 regexp-oneliner 到来之前,我会使用老式状态机:
bundle = {}
with open("config.txt") as f:
for line in f:
line=line.strip().split()
if line:
if line[0] == "package":
package = {}
bundle[line[1]] = package
elif line[0] == "config":
config = {}
package[" ".join(line[1:])] = config
elif line[0] == "option":
config[line[1]] = " ".join(line[2:])
print(bundle)
结果是
{'autoload': {"core 'main'": {'Enabled': "'no'", 'StartTimer': "'120'", 'RetryTimer': "'30'", 'BackoffTimer': "'15'", 'BootUsingConfig': "'altconfig'", 'BootUsingImage': "'altimage'"}, 'entry': {'Configured': "'yes'", 'SegmentName': "'altconfig'", 'RemoteFilename': "'$$.ini'"}}, 'cwatch': {"watch '3g_watch'": {'enabled': "'yes'", 'test_ifaces': "'wan1 wan2'", 'failure_time_1': "'30m'", 'failure_action_1': "'reboot'"}}}