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