剛剛看sumo的官方教程,有一段代碼是生成配置文件,發現python中的print函數也可以寫文件。
with open("data/cross.rou.xml", "w") as routes: print("""<routes> <vType id="typeWE" accel="0.8" decel="4.5" sigma="0.5" length="5" minGap="2.5" maxSpeed="16.67" \ guiShape="passenger"/> <vType id="typeNS" accel="0.8" decel="4.5" sigma="0.5" length="7" minGap="3" maxSpeed="25" guiShape="bus"/> <route id="right" edges="51o 1i 2o 52i" /> <route id="left" edges="52o 2i 1o 51i" /> <route id="down" edges="54o 4i 3o 53i" />""", file=routes)
其中三引號的作用之前文章寫過,是為了保證代碼中的換行效果能正常導出