我只是按照一个简单的Python脚本来编写一个文本文件。suggetsed方法;在结尾添加“\n”无效。它是在一个循环内打印当我使用Windows时,我也尝试了“\r\n”。它仍然只打印最后一个项目。我尝试过在循环内外移动所有内容(从path
开始,以file.close()
结束),但没有成功。这是怎么回事?你知道吗
#Assign variables to the shapefiles
park = "Parks_sd.shp"
school = "Schools_sd.shp"
sewer = "Sewer_Main_sd.shp"
#Create a list of shapefile variables
shapeList = [park, school, sewer]
path = r"C:/EsriTraining/PythEveryone/CreatingScripts/SanDiegoUpd.txt"
open(path, 'w')
for shp in shapeList:
shp = shp.replace("sd", "SD")
print shp
file = open(path, 'w')
file.write(shp + "\r\n")
file.close()
您可以1)在for循环外打开文件,2)使用writeline
或者
这样,您只需打开一次文件,一旦写入行,文件就会自动关闭(因为[with])。你知道吗
在循环外打开文件
例如:
相关问题 更多 >
编程相关推荐