2

I'm trying to write a Python script that does a process (in this case a buffer) iteratively. The input layer is always the same but, in each iteration, the name of the output file must change and It must be exported to gpkg.

This is the structure of the string that I need to create in order to pass it as parameter:

'ogr:dbname=\'/dir/layer.gpkg\' table=\"layername\" (geom) sql='

This string must be creaeted from two parameters: a path and a filename.

I'm struggling with those internal single/double quotes and the backslashes because I don't know how they behave. This is what I have tried until now:

path = './output/layer.gpkg'
dbname = f"""{path}"""
filename = 'filename'
outputlayer = """ogr:dbname='""" + dbname + """' table="layername" (geom) sql="""
outputlayer

And this is what I get:

'ogr:dbname=\'./output/layer.gpkg\' table="layername" (geom) sql='

I need to parametrize the layername part and put those double quotes as shown above.

Just to put it in contest: My final goal is to create a parametrize string that works within this 'OUTPUT' (outputlayer):

processing.run("native:buffer", {
               'INPUT': inputlayer,
               'DISTANCE': buffer, 'SEGMENTS': 20, 'END_CAP_STYLE': 0, 'JOIN_STYLE': 0, 'MITER_LIMIT': 2,
               'DISSOLVE': bufferdiss,
               'OUTPUT': outputlayer})

Can you provide a cheatsheet or some guide to work with these complex strings?

  • 2
    Have you tried Python3 f-strings : f"ogr:dbname='{dbname}' table='{layername}' (geom) sql=" – J. Monticolo Feb 4 '20 at 10:50
  • That gives me this: "ogr:dbname='./input/layer.gpkg' table='filename' (geom) sql=" May be the buffer part works just fine with that string but at a glance I see that it lacks the backslashes – César Arquero Feb 4 '20 at 11:09
  • I have the answer but, I don't know exactly why, this has been closed...I still think this is absolutely related to QGIS and pyqgis... – César Arquero Feb 11 '20 at 7:40

Browse other questions tagged or ask your own question.