【发布时间】:2014-12-28 02:14:20
【问题描述】:
我有一个文件(请参阅下面代码中的状态变量),我想将其转换为流程图(附件)。我的 python 脚本将“状态”转换为字典。如何将该字典转换为流程图或图形? 我的代码:
status = """
Object car {
Name honda;
From Richland;
To Seattle;
Distance 160;
Configuration road_travel;
}
Object bus {
Name greyhound;
From pasco;
To richland;
Distance 15;
Configuration road_travel;
}
Object aeroplane {
Name united;
From miami_airport;
To pasco;
Distance 1000;
Configuration air_travel;
}
Object train {
Name gas_train;
From beach;
To miami_airport;
Distance 30;
Configuration train_travel;
}
"""
sale_number = ''
sales = collections.defaultdict(list)
for line in status.split('\n'):
line = line.strip()
if line.startswith("set"):
continue
elif (line.startswith("Object") or line.startswith("object")):
sale_number = line.split(' ')[1].strip()
elif not line or line.isspace() :
continue
else:
# you can also use a regular expression here
sales[sale_number].append(line.split())
for sale in sales:
print sale+str(dict(sales[sale][:-1]))
这会产生:
car{'To': 'Seattle;', 'Configuration': 'road_travel;', 'From': 'Richland;', 'Name': 'honda;', 'Distance': '160;'}
train{'To': 'miami_airport;', 'Configuration': 'train_travel;', 'From': 'beach;', 'Name': 'gas_train;', 'Distance': '30;'}
aeroplane{'To': 'pasco;', 'Configuration': 'air_travel;', 'From': 'miami_airport;', 'Name': 'united;', 'Distance': '1000;'}
bus{'To': 'richland;', 'Configuration': 'road_travel;', 'From': 'pasco;', 'Name': 'greyhound;', 'Distance': '15;'}
我想将上面的 python 输出转换成如下图所示的图片。我不想使用 Giffy 或 MS-Visio 手动执行此操作,因为我的实际案例有大约 1000 个对象(此示例在“状态”中有 4 个对象)
【问题讨论】:
-
@Jivan 我一直在做的事情是我编写了一个 python 脚本,将具有“状态”内容的文件转换为 .dot 文件,然后将其导入到 graphviz 中以对其进行可视化。随着对象数量的增加,这变得庞大而复杂,因此我想将内容转换为图形消除 graphviz 方法。到目前为止,我将文件内容转换为字典,我想将“key”和“values”导入 tkinter 或 D3 或其他一些可视化库(python 或 javascript),但我不确定如何做到这一点。
-
@Jivan 我也尝试按照pythonhaven.wordpress.com/2009/12/09/… 中的示例使用“pydot”库,但其中很多都涉及硬编码,这对这个问题没有帮助
标签: javascript python dictionary tkinter flowchart