简体   繁体   English

如何在QGIS / Python中循环使用.format和变量

[英]How to use .format with variables in loop with QGIS/Python

I've got the following code, but I can't quite get it to work: 我有以下代码,但无法完全正常工作:

import glob
import os
import processing # for QGIS functionality

os.chdir("/home/mydir/vector")

columnid = ['SAMP300','SAMP100','SAMP30', 'SAMP10']

for x in glob.glob("prefix*.shp"):
    for y in columnid:
        for z in [1, 2, 3]:
            output_alg0='"/home/mydir/vector/samples/{x}_{y}_{z}.shp"'.format(x=x.rstrip('.shp'), y=y, z=z)
            output_0='processing.runalg("qgis:randompointsinsidepolygonsvariable","/home/mydir/vector/{x}",0,"{y}",1000,output_alg0)'.format(x=x, y=y)

The script reads through a directory of .shp files, and uses variables to name the outputs. 该脚本读取.shp文件的目录,并使用变量来命名输出。 output_alg0 creates the output file name used in the next step. output_alg0创建在下一步中使用的输出文件名。 The file name is based on the original file, and two variables within the loop. 文件名基于原始文件以及循环中的两个变量。 output_0 is the actual QGIS algorithm that is run, which references each .shp file in the loop, passes the columnid variable and some fixed parameters, and references output_alg0 for naming the output .shp file. output_0是实际运行的QGIS算法,它引用循环中的每个.shp文件,传递columnid变量和一些固定参数,并引用output_alg0来命名输出.shp文件。

If I append print in front of the two commands within the loop, I get the output that I'm expecting (ie, the {x},{y}, and {z} variables are correctly populated. Furthermore, the script gives no error when executed, but no output is produced. 如果我在循环中的两个命令前面附加了print ,则会得到期望的输出(即{x},{y}和{z}变量已正确填充。此外,脚本没有给出执行时出错,但不产生任何输出。

Here's an example of the output by appending print and parentheses to the two lines within the loop: 这是通过将print和括号附加到循环中的两行的输出示例:

output_alg0="/home/mydir/vector/samples/prefix_SAMP10_3.shp"
output_0=processing.runalg("qgis:randompointsinsidepolygonsvariable","/home/mydir/vector/prefix.shp",0,"SAMP10",1000,output_alg0)

I can copy and paste both lines exactly as they appear above into the QGIS Python Console and the commands are executed as expected (ie, random point file is generated based on the input shapefile, and an output shapefile is created as specified). 我可以将上述两行完全复制并粘贴到QGIS Python控制台中,并按预期执行命令(即,根据输入shapefile生成随机点文件,并根据指定创建输出shapefile)。

I think it has something to do with how I'm using .format and/or perhaps how I'm using single and/or double quotations, and/or some sort of Python/QGIS interaction that I don't quite understand. 我认为这与我如何使用.format和/或我如何使用单引号和/或双引号以及/或某种我不太了解的Python / QGIS交互有关。

EDIT : Instead of using the two output_ prefixes, I also tried out this one-liner version, and the script executes (without error), but no output is created. 编辑 :而不是使用两个output_前缀,我还尝试了这种单行版本,脚本执行了(没有错误),但是没有创建输出。

import glob
import os
import processing # for QGIS functionality

os.chdir("/home/mydir/vector")

columnid = ['SAMP300','SAMP100','SAMP30', 'SAMP10']

for x in glob.glob("prefix*.shp"):
    for y in columnid:
        for z in [1, 2, 3]:
           'processing.runalg("qgis:randompointsinsidepolygonsvariable","/home/mydir/vector/{w}",0,"{y}",1000,"/home/mydir/vectorsamples/{x}_{y}_{z}.shp")'.format(w=x, x=x.rstrip('.shp'), y=y, z=z)

Currently your loop is generating the variables how you want them to and assigning them to output_alg0 and output_0 the way you want, but you don't ever DO anything with them after that. 当前,您的循环正在生成所需变量的方式,然后output_0所需方式将其分配给output_alg0output_0 ,但是output_0您将不再对其执行任何操作。

I've asked for your expected output in a comment. 我已在评论中要求您提供预期的输出。 If you add that we can probably figure out how to get you there. 如果您补充,我们大概可以弄清楚如何到达那里。 My GUESS is that you're trying to execute output_0 (which currently is just a string that looks like a python command). 我的GUESS是您正在尝试执行output_0 (当前只是一个看起来像python命令的字符串)。 In which case you'd have to use exec(output_0) but be wary of using exec in general. 在这种情况下,您将不得不使用exec(output_0)但要谨慎使用exec It's a scary command. 这是一个可怕的命令。

EDIT: 编辑:

If I were you, I'd do something more like: 如果我是你,我会做更多的事情:

# inside your loops::
output_alg0='"/home/mydir/vector/samples/{x}_{y}_{z}.shp"'.format(x=x.rstrip('.shp'), y=y, z=z)
output_0 = processing.runalg("qgis:randompointsinsidepolygonsvariable", "/home/mydir/vector/{x}".format(x=x), 0, y, 1000, output_alg0)

That way you're running the script directly rather than writing a bit of code then executing it. 这样,您就可以直接运行脚本,而不是编写一些代码然后执行它。

Changed my EDIT -ed line written as: 更改了我的EDIT编辑行,写为:

'processing.runalg("qgis:randompointsinsidepolygonsvariable","/home/mydir/vector/{w}",0,"{y}",1000,"/home/mydir/vectorsamples/{x}_{y}_{z}.shp")'.format(w=x, x=x.rstrip('.shp'), y=y, z=z)

to this, which wraps exec with quotations marks around the line above: 为此,它用引号将exec包裹在上面的行周围:

exec('processing.runalg("qgis:randompointsinsidepolygonsvariable","/home/mydir/vector/{w}",0,"{y}",1000,"/home/mydir/vectorsamples/{x}_{y}_{z}.shp")'.format(w=x, x=x.rstrip('.shp'), y=y, z=z))

The above modification to the script is running and iterates through the list of .shp files within the specified directory; 上面对脚本的修改正在运行,并且在指定目录内的.shp文件列表中进行迭代。 however, I don't think this is the most kosher answer possible. 但是,我认为这不是最简洁的答案。 I'll hold off accepting my own answer if something better and/or more QGIS-friendly comes along. 如果出现更好和/或更友好的QGIS,我将拒绝接受自己的答案。

PS-thanks to @Adam Smith for pointing out exec PS感谢@Adam Smith指出exec

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

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