0
votes

I need to construct a chain of text like this:

 out = 'ogr:dbname=\'C:\\output\\2020.gpkg\' table=\"2020\" (geom) sql='

Here is my code:

import glob, time, sys, threading, os
from datetime import date, timedelta, datetime
import time, threading

#Parameters
layer = 'C:\\layer.gpkg' 
n ='2020'
outdir = 'C:\\output'

#Process
l = os.path.realpath(layer)
pn = os.path.realpath(outdir + '/' + n + '.gpkg')
p = f"'{pn}'"
f = f"'{n}'"
o = f'ogr:dbname={p} table={f} (geom) sql='

#Test
out = 'ogr:dbname=\'C:\\output\\2020.gpkg\' table=\"2020\" (geom) sql='
o == out

The goal is to get o == out.

What do I need to change in the #Process part in order to get this as True ?

Moreover I need to run this either in linux or windows.

My final goal is to create a function that give 3 strings returns the complex string line shown above.

3
"I don't even understand why the output changes single quotes to double quotes." Inside a string this matters. But if you're talking about the quotes defining the string itself this is irrelevant. Try 'foo' == "foo". - Chris
There's no difference between single quote and double quote for defining a string in python. But using them in mixed mode can save you some backslashes sometimes. You need string formatting. A simple example with f string. f"org:dbname='{trypath}'". - Psidom
Are you sure the string you're trying to generate should escape both single and double quotes? Do you want those backslashes to actually be contained in the final string? - Chris
Going to the point I need to solve the \'/ part. \ must come from one string and '/path' from another. - César Arquero
Try outputlayer = '\'ogr:dbname=\\' + '\'' + trypath + '\'' - Giannis Clipper

3 Answers

2
votes

Assuming you are using python 3.6 or above you should use format strings (also known as f strings) to construct strings from variables. Start the string with the letter "f" and then put whatever variables you want in curly brackets {}. Also if you use single quotes as the outer quote then you don't have to escape double quotes and vice versa.

Code:

db_name = "'home/user/output/prueba.gpkg'"
table_name = '"prueba"'
outputlayer = f'ogr:dbname={db_name} table={table_name} (geom) sql='
outputlayer

Output:

'ogr:dbname=\'home/user/output/prueba.gpkg\' table="prueba" (geom) sql='
1
votes

I think one of the issues that this isn't working is your path here pn = os.path.realpath(outdir + '/' + n + '.gpkg'). This is trying to combine UNIX path / with windows path \\. A more robust solution in terms of portability between linux and windows would be to use the path.join function in os module.

Additionally, python f strings will only add escapes to whichever quote character you used to open the string (' or "). If the escaped quotes around both strings are necessary, you're probably better off hard coding it into an f-string instead of setting 2 new variables with different quote types.

import glob, time, sys, threading, os
from datetime import date, timedelta, datetime
import time, threading

#Parameters
layer = 'C:\\layer.gpkg' 
n ='2020'
outdir = 'C:\\output'

#Process
l = os.path.realpath(layer)
pn = os.path.realpath(os.path.join(outdir, f"{n}.gpkg"))
o = f'ogr:dbname=\'{pn}\' table=\"{n}\" (geom) sql='

#Test
out = 'ogr:dbname=\'C:\\output\\2020.gpkg\' table=\"2020\" (geom) sql='
o == out

A version of this (different path) has been tested to work on my linux machine.

1
votes

Another option is to use a triple quoted string:

dbname = """/home/user/output/prueba.gpkg"""
outputlayer = """ogr:dbname='"""+dbname+"""' table="prueba" (geom) sql="""

which gives:

'ogr:dbname=\'/home/user/output/prueba.gpkg\' table="prueba" (geom) sql='