Python的初学者,我真的有一个文本文件中挣扎我要打印:在python中,你如何编写一个包含brakets和引号的文本文件?
{"geometry": {"type": "Point", "coordinates":
[127.03790738341824,-21.727244054924235]}, "type": "Feature", "properties": {}}
具有多个括号让我感到困惑的事实,并尝试此方法后抛出Syntax Error
:
def test():
f = open('helloworld.txt','w')
lat_test = vehicle.location.global_relative_frame.lat
lon_test = vehicle.location.global_relative_frame.lon
f.write("{"geometry": {"type": "Point", "coordinates": [%s, %s]}, "type": "Feature", "properties": {}}" % (str(lat_test), str(lat_test)))
f.close()
由于你可以看到,我有我自己的经度和纬度变量,但python是抛出一个语法错误:
File "hello.py", line 90
f.write("{"geometry": {"type": "Point", "coordinates": [%s, %s]}, "type":
"Feature"" % (str(lat_test), str(lat_test)))
^
SyntaxError: invalid syntax
谢谢提前有很多的帮助。
您的文本文件是[JSON](http://www.json.org/)格式吗? –
https://stackoverflow.com/a/12309296/5538805 – MrPyCharm
该文件的实际格式将是geojson。我想我只是将扩展名从txt更改为js – Diamondx