简体   繁体   English

如何在 for 循环中更改 output 文件名? ArcPy

[英]How to change output filename in a for loop? ArcPy

I am using an ArcPy function (ExtractByAttributes) in a for loop.我在 for 循环中使用 ArcPy function (ExtractByAttributes)。 I have a series of files names ("i") in a list (test).我在列表(测试)中有一系列文件名(“i”)。 They are rasters for which I want to run a process in ArcPy.它们是我想在 ArcPy 中运行进程的栅格。

The for loop basically builds the path to find each raster in the first line and then runs the ArcPy process (Extract by Attributes). for 循环基本上构建路径以在第一行中查找每个栅格,然后运行 ArcPy 进程(按属性提取)。 As written now, the loop runs correctly but in each iteration it overwrites the result file.如现在所写,循环运行正确,但在每次迭代中它都会覆盖结果文件。 How can I rewrite this so the variable "result" has a different name in every iteration?我如何重写它以便变量“结果”在每次迭代中都有不同的名称?

for i in test:
    path = folder_path + "\\" + i + "\\" + i + '.tif'
    result = ExtractByAttributes(path,"VALUE = 10 OR VALUE = 30")

Thanks a lot in advance:)非常感谢:)

You could make an array of the same length and store the results in there.您可以制作一个相同长度的数组并将结果存储在其中。

results = [len(test)]
for i in range(len(test)):
    path = folder_path + "\\" + test[i] + "\\" + i + '.tif'
    results[i] = ExtractByAttributes(path,"VALUE = 10 OR VALUE = 30")

Solved:解决了:

for i in rasters:
    path = folder_path + "\\" + i + "\\" + i + '.tif'
    print(path)
    result = ExtractByAttributes(path,"VALUE = 10 OR VALUE = 30")
    result.save(folder_path + "\\" + str(i) + ".tif")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM