python使用print写文件

刚刚看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)

其中三引号的作用之前文章写过,是为了保证代码中的换行效果能正常导出

Python中单引号、双引号、三引号的区别

posted @ 2019-02-19 11:22  roadwide  阅读(4443)  评论(0编辑  收藏  举报