我有一个python代码,它从Input文件夹中提取2个输入文件,它们是shapefile,但是格式与问题无关。我处理这些文件,然后最终将一个csv文件输出到Output文件夹。
为使这一过程形象化,流线可以解释我的工作。
Input/Lines/line1.shp + Input/Points/point1.shp >> Python Code >> Output/data1.csv
我希望能够遍历输入文件夹中所有输入文件的python代码并存储所有输出文件。因此,line1.shp + point1.shp = data1.csv和line2.shp + point2.shp = data2.csv,依此类推。
关于如何执行此操作的任何指导或帮助都将非常有用,代码可以很好地运行任何输入,但希望能够对大量数据进行处理。我是python的新手,并不真正知道要寻找什么。
谢谢!
答案 0 :(得分:0)
我认为for循环将对您有所帮助:
>>> import shapefile
>>> import csv
>>> file_point = 'Input/points/point%s.shp'
>>> file_line = 'Input/Lines/line%s.shp'
>>> file_output = 'Output/data%s.csv'
>>>
>>> number_of_file = 10 # your file size
>>>
>>> for file_index in range(1,number_of_file+1):
... sf_p = shapefile.Reader(file_point%str(file_index))
... sf_l = shapefile.Reader(file_line%str(file_index))
...
... # do some things you want
...
... with open(file_output%str(file_index), 'w') as csvfile:
... # write(your data)
# example of file pathes in for loop
Input/points/point1.shp
Input/points/point2.shp
Input/points/point3.shp
Input/points/point4.shp
Input/points/point5.shp
Input/points/point6.shp
Input/points/point7.shp
....
这两个网站将对python中的shapfile和csv读取器/写入器很有帮助
答案 1 :(得分:0)
首先,我不确定您需要对文件做些什么,但是看来您已经了解了。 os module的某些部分可能会有用。我提供了对您而言似乎最有用的功能。
# os module
import os
# get the current path where your program is executing
current_path = os.getcwd()
# or
current_path = 'my/file/path/to/files'
# lists all the files and directories in the current path
for item in os.listdir(current_path):
# the file path for each item in current_path
file_path = current_path+"\\"+item
# make sure the path is not a directory
if not os.path.isdir(file_path):
# separate the file name and file extension
file_name, file_extension = os.path.splitext(file_path)
# make sure you are operating on .shp files
if file_extension == '.shp':
# do you file processing here
# also create you output files here
# if you want 1 output for every input file