當我運行我的代碼,我得到以下的輸出:停止循環創建不需要的代碼?
<Placemark>
<description>Picture<![CDATA[<img src='./files/picOne.jpg' width='400' height='300'>]]></description>
<styleUrl>#Photo</styleUrl>
<coordinates>-1.57245646667, 53.8090789556</coordinates>
</Placemark>
<styleUrl>#Photo</styleUrl>
<coordinates>-1.56958587633, 53.8345916667</coordinates>
</Placemark>
<Placemark>
<description>Picture<![CDATA[<img src='./files/picTwo.jpg' width='400' height='300'>]]></description>
<styleUrl>#Photo</styleUrl>
<coordinates>-1.57245646667, 53.8090789556</coordinates>
</Placemark>
<styleUrl>#Photo</styleUrl>
<coordinates>-1.56958333333, 53.8111916667</coordinates>
</Placemark>
</Folder>
</Document>
</kml>
我想txt文件的輸出如下:
<Placemark>
<description>Picture<![CDATA[<img src='./files/picOne.jpg' width='400' height='300'>]]></description>
<styleUrl>#Photo</styleUrl>
<coordinates>-1.57245646667, 53.8090789556</coordinates>
</Placemark>
<Placemark>
<description>Picture<![CDATA[<img src='./files/picTwo.jpg' width='400' height='300'>]]></description>
<styleUrl>#Photo</styleUrl>
<coordinates>-1.57245646667, 53.8090789556</coordinates>
</Placemark>
我不知道爲什麼我的腳本產生這種額外的每個循環之後的代碼位。
**<styleUrl>#Photo</styleUrl>
<coordinates>-1.57245646667, 53.8111916667</coordinates>
</Placemark>**
下面是代碼:
oneLat = ['53.8290433456','53.8346016367']
oneLong = ['-1.57245646667', '1.56959975983']
picList = ['picOne.jpg', 'picTwo.jpg']
coordPairs = zip((-float(x) for x in oneLong), (float(x) for x in oneLat))
with open("temp.txt",'w') as f:
increment = 0
while increment < len(picList):
f.write("\n\n<Placemark>\n")
f.write("\t\t\t<description>Picture<![CDATA[<img src='./files/{}' width='400' height='300'>]]></description>\n".format(picList[increment]))
increment = increment + 1
for coord in coordPairs:
f.write("\t\t<styleUrl>#Photo</styleUrl>\n")
f.write("\t\t<coordinates>{}, {}</coordinates>\n".format(*coord))
f.write("</Placemark>\n\n")
f.write('''
</Folder>
</Document>
</kml>''')
如果有人可以幫助這將是非常讚賞
嗯是的 - 但我不希望它生成該代碼。我問我怎麼能做到我想要的:) – BubbleMonster