4

How to replace a string with a variable inside a pgsql2shp command in Python?

command = "pgsql2shp -f /test.shp  -h localhost -u postgres -P postgres gis_exercise  \"SELECT d.latitude, d.longitude, d.geom FROM country AS c, daily AS d WHERE ST_CONTAINS(c.geom,d.geom) AND c.iso3= 'BRA'\""

What I need is to replace the test (name of shapefile) with the variable row and also the string BRA with the variable row.

I have tried to use + in order to concatenate the strings but it doesn't work.

1 Answer 1

4

You may use string .format() method:

command = "pgsql2shp -f /{}.shp  -h localhost -u postgres -P postgres gis_exercise  \"SELECT d.latitude, d.longitude, d.geom FROM country AS c, daily AS d WHERE ST_CONTAINS(c.geom,d.geom) AND c.iso3= '{}'\"".format(row[0], row[1])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.