1

我有以下代码,它使用我需要的数据从 shp.file 创建我需要的 txt 文件。我有一个名为profiles 的文件夹,其中包含一些名为(profil1.shp、profil2.shp、profil3.shp 等)的形状文件。我想知道如何创建一个循环,以便脚本为每个文件创建一个具有相同名称的 txt 文件(例如,对于profil1.shp 创建profil1.txt,profil2.shp 创建profil2.txt 等等)。

import ogr, os, sys, osr

os.chdir('..\profiles')

file = open('profil1.txt', 'w')

driver = ogr.GetDriverByName('ESRI Shapefile')

datasource = driver.Open('profil1.shp', 0)
if datasource is None:
  print 'Could not open file'
  sys.exit(1)

layer = datasource.GetLayer()

feature = layer.GetNextFeature()
while feature:

  id = feature.GetFieldAsString('ID')
  Distanta = feature.GetFieldAsString('DIST')
  Z = feature.GetFieldAsString('Z')
  geom = feature.GetGeometryRef()
  x = str(geom.GetX())
  y = str(geom.GetY())

  file.write(id + " " + Distanta + " " + "[X]:" + " " + x + ' ' + '[Y]:' + " "  + y + " " + " " + "[Z]" + Z + " " +  "\n")

  feature.Destroy()
  feature = layer.GetNextFeature()

datasource.Destroy()
file.close()

编辑:代码返回一个无法打开的文件。包含文件及其各自名称的文件夹的照片。可以安全地假设我做错了什么。

import ogr, os, sys, osr,os.path
os.chdir = ('C:\Users\Andrei\Desktop\profil3')
l = os.listdir('C:\Users\Andrei\Desktop\profil3')
for i in l:
    if i.endswith('.shp'):
        s1 = s.split('.')[0] + '.txt'
        file = open(s1, 'w')

    driver = ogr.GetDriverByName('ESRI Shapefile')
    datasource = driver.Open(i, 0)
    if datasource is None:
        print 'Could not open file'
        sys.exit(1)

    layer = datasource.GetLayer()

    feature = layer.GetNextFeature()
    while feature:

        id = feature.GetFieldAsString('ID')
        Distanta = feature.GetFieldAsString('DIST')
        Z = feature.GetFieldAsString('Z')
        geom = feature.GetGeometryRef()
        x = str(geom.GetX())
        y = str(geom.GetY())

        file.write(id + " " + Distanta + " " + "[X]:" + " " + x + ' ' + '[Y]:' + " "  + y + " " + " " + "[Z]" + Z + " " +  "\n")

        feature.Destroy()
        feature = layer.GetNextFeature()

    datasource.Destroy()
    file.close()
4

1 回答 1

0

您可以使用os.listdir()列出当前目录中的文件和文件夹。这将返回当前目录中所有文件的列表(或作为参数提供给它的目录,如果没有指定参数,它会检查当前目录)。

.shp然后您可以检查名称以using function结尾的文件,string.endswith()然后使用它来创建新文件。

一小部分的例子 -

import os , os.path
l = os.listdir()
for i in l:
    if i.endswith('.shp'):
        s1 = s.split('.')[0] + '.txt'

最后 s1 将包含扩展名为.txt.

然后你可以在这个文件上做你的逻辑,并继续这样做。

完整的代码看起来像 -

导入 ogr, os, sys, osr,os.path

os.chdir('..\profiles') l = os.listdir() for i in l: if i.endswith('.shp'): s1 = s.split('.')[0] + '. txt'文件=打开(s1,'w')

    driver = ogr.GetDriverByName('ESRI Shapefile')

    datasource = driver.Open(i, 0)
    if datasource is None:
        print 'Could not open file'
        sys.exit(1)

    layer = datasource.GetLayer()

    feature = layer.GetNextFeature()
    while feature:

        id = feature.GetFieldAsString('ID')
        Distanta = feature.GetFieldAsString('DIST')
        Z = feature.GetFieldAsString('Z')
        geom = feature.GetGeometryRef()
        x = str(geom.GetX())
        y = str(geom.GetY())

        file.write(id + " " + Distanta + " " + "[X]:" + " " + x + ' ' + '[Y]:' + " "  + y + " " + " " + "[Z]" + Z + " " +  "\n")

        feature.Destroy()
        feature = layer.GetNextFeature()

    datasource.Destroy()
    file.close()

打开文件等的更好方法是使用with语句。在这里查看它的教程。

于 2015-06-28T13:12:51.757 回答