CSV.writer |确保 CSV 中的第一行也是最后一行
CSV.writer | Ensure first row in CSV is also last row
此代码遍历共享驱动器上的 .KAP 文件,并在我的本地驱动器上创建 CSV,其中仅包含 .KAP 文件中存在的 lat/lon 坐标对。
.KAP 文件中的坐标线示例
PLY/1,48.107478621032,-69.733975000000
PLY/2,48.163516399836,-70.032838888053
PLY/3,48.270000002883,-70.032838888053
PLY/4,48.107478621032,-69.733975000000
这些坐标稍后将用于创建多边形。
问题:大约一半的 .KAP 文件还会将第一个 lat/lon 坐标对保留为最后一个 lat/lon 坐标对。 (对于闭合多边形非常有用,请参阅上面的 PLY/1 和 PLY/4 是如何相同的)。由于某些原因,一些 .KAP 文件没有这个。看到我要去哪里了吗?
如何确保第一个坐标对始终记录为所有输出 CSV 中的最后一个坐标对?
到目前为止,这是我的代码:
import os
import zipfile
import csv
currentPath = r"K:\BsbProd\BSBCHS\BSB_4_Release"
for zip in [i for i in os.listdir(currentPath) if i.lower().endswith('.zip')]:
zf = zipfile.ZipFile(os.path.join(currentPath, zip), 'r')
for each in [n for n in zf.namelist() if n.lower().endswith('.kap')]:
currentFile = zf.open(each).readlines()
writer = csv.writer(open(r"C:\Users\PalmerDa\Desktop\BSB\{}.csv".format(os.path.basename(each)), "wb"))
for row in currentFile:
if row.startswith('PLY'):
col1, col2 = row.split(",")[1:]
rows = float(col1), float(col2)
writer.writerow(rows)
只需对最后一行进行某种检查,看它是否与第一行相同,如果不相同,则将第一行的副本添加到末尾。也许像
for i in range(len(currentFile)):
row = currentfile[i]
if(row.startswith('PLY')):
col1, col2 = row.split(",")[1:]
rows = float(col1), float(col2)
writer.writerow(rows)
if(i = len(currentFile) and row != currentfile[0]):
firstcol1, firstcol2 = currentfile[0].split(',')[1:]
firstrow = float(firstcol1), float(firstcol2)
writer.writerow(firstrow)
或者更简单,只需在阅读行后查看任何行之前将第一行添加到末尾:
currentFile = zf.open(each).readlines()
if(currentFile[-1] != currentFile[0]): currentFile.append(currentFile[0])
然后您根本不需要更改行循环。我更喜欢第二种方案
当我有时间去做时,我想出了这个解决我自己问题的方法:
import os
import zipfile
import csv
import glob
currentPath = r"K:\BsbProd\BSBCHS\BSB_4_Release"
for zip in glob.glob('{}/*.zip'.format(currentPath)):
zf = zipfile.ZipFile(os.path.join(currentPath, zip), 'r')
for each in [n for n in zf.namelist() if n.lower().endswith('.kap')]:
currentFile = zf.open(each).readlines()
writer = csv.writer(open(r"C:\Users\PalmerDa\Desktop\BSB2{}.csv".format(os.path.basename(each)), "wb"))
plys = [r for r in currentFile if r.startswith('PLY')]
for rr in plys:
col1, col2 = rr.split(",")[1:]
rows = float(col1), float(col2)
writer.writerow(rows)
if plys[0] != plys[-1]:
writer.writerow((float(plys[0].split(",")[1]),float(plys[0].split(",")[2])))
此代码遍历共享驱动器上的 .KAP 文件,并在我的本地驱动器上创建 CSV,其中仅包含 .KAP 文件中存在的 lat/lon 坐标对。
.KAP 文件中的坐标线示例
PLY/1,48.107478621032,-69.733975000000
PLY/2,48.163516399836,-70.032838888053
PLY/3,48.270000002883,-70.032838888053
PLY/4,48.107478621032,-69.733975000000
这些坐标稍后将用于创建多边形。
问题:大约一半的 .KAP 文件还会将第一个 lat/lon 坐标对保留为最后一个 lat/lon 坐标对。 (对于闭合多边形非常有用,请参阅上面的 PLY/1 和 PLY/4 是如何相同的)。由于某些原因,一些 .KAP 文件没有这个。看到我要去哪里了吗?
如何确保第一个坐标对始终记录为所有输出 CSV 中的最后一个坐标对?
到目前为止,这是我的代码:
import os
import zipfile
import csv
currentPath = r"K:\BsbProd\BSBCHS\BSB_4_Release"
for zip in [i for i in os.listdir(currentPath) if i.lower().endswith('.zip')]:
zf = zipfile.ZipFile(os.path.join(currentPath, zip), 'r')
for each in [n for n in zf.namelist() if n.lower().endswith('.kap')]:
currentFile = zf.open(each).readlines()
writer = csv.writer(open(r"C:\Users\PalmerDa\Desktop\BSB\{}.csv".format(os.path.basename(each)), "wb"))
for row in currentFile:
if row.startswith('PLY'):
col1, col2 = row.split(",")[1:]
rows = float(col1), float(col2)
writer.writerow(rows)
只需对最后一行进行某种检查,看它是否与第一行相同,如果不相同,则将第一行的副本添加到末尾。也许像
for i in range(len(currentFile)):
row = currentfile[i]
if(row.startswith('PLY')):
col1, col2 = row.split(",")[1:]
rows = float(col1), float(col2)
writer.writerow(rows)
if(i = len(currentFile) and row != currentfile[0]):
firstcol1, firstcol2 = currentfile[0].split(',')[1:]
firstrow = float(firstcol1), float(firstcol2)
writer.writerow(firstrow)
或者更简单,只需在阅读行后查看任何行之前将第一行添加到末尾:
currentFile = zf.open(each).readlines()
if(currentFile[-1] != currentFile[0]): currentFile.append(currentFile[0])
然后您根本不需要更改行循环。我更喜欢第二种方案
当我有时间去做时,我想出了这个解决我自己问题的方法:
import os
import zipfile
import csv
import glob
currentPath = r"K:\BsbProd\BSBCHS\BSB_4_Release"
for zip in glob.glob('{}/*.zip'.format(currentPath)):
zf = zipfile.ZipFile(os.path.join(currentPath, zip), 'r')
for each in [n for n in zf.namelist() if n.lower().endswith('.kap')]:
currentFile = zf.open(each).readlines()
writer = csv.writer(open(r"C:\Users\PalmerDa\Desktop\BSB2{}.csv".format(os.path.basename(each)), "wb"))
plys = [r for r in currentFile if r.startswith('PLY')]
for rr in plys:
col1, col2 = rr.split(",")[1:]
rows = float(col1), float(col2)
writer.writerow(rows)
if plys[0] != plys[-1]:
writer.writerow((float(plys[0].split(",")[1]),float(plys[0].split(",")[2])))