[Tutor] PYTHON QUOTES ISSUE

Susana Iraiis Delgado Rodriguez susana.delgado_s at utzmg.edu.mx
Fri Oct 8 16:27:25 CEST 2010


Hi Alan:

The ouput is coming from a cicle and some functions that I vae to do to
execute an ogr2ogr command, in this output I ask the user for the name of a
file and then make a module to get to the subprocess part:

import shlex, subprocess, sys
from dbf import *
def process():
#Read dbfile status 100%
     a = open ("capas.txt","w+")
     print 'Enter the shapefile name'
     b = raw_input()
     print '\n'
    dbf = Dbf(b+".dbf",new=False)

    for rec in dbf:
         for fldName in dbf.fieldNames:
             if fldName == 'LAYER':
             l=()
             l=rec[fldName]
             a.write(l)
             a.write("\n")
    a.close()

##Eliminate duplicate lines from the txt into a new txt, status:100%
      a = open ("capas.txt","r")
      catalogo = open ("unico.txt","w")
      unique = set(a.read().split("\n"))
      catalogo.write("".join([line + "\n" for line in unique]))
      catalogo.close()
      a.close()

##Execute ogr2ogr command, status:75%
     for line in open("unico.txt", "r"):
        p = subprocess.Popen(['C:/Archivos de
programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where',
"\"LAYER='"+line+"'\"" , b+'.shp'])

But when I executed it shows me an error in the layer's name:

>>> ERROR 1: Failed to identify field:LAYER=
ERROR 1: Failed to create file .shp file.
ERROR 4: Failed to open Shapefile `0
.shp'.

I think the erros showed up because some of the layer's values are 0 and '
', and obsviously you can't create a file from nothing on it. But I don`t
know how to validate if a layer's value is equals to 0 or ' ', any idea what
I'm doing wrong or how to fix it?


2010/10/7 Susana Iraiis Delgado Rodriguez <susana.delgado_s at utzmg.edu.mx>

> Hello taserian and Antonio!
>
> Thank you both for taking the time to answer my question. With taserian's
> code it gives me the next output:
> C... ogr2ogr T21-PUENTESshp -where "LAYER=T21-PUENTES"
> tapalpa_05_plani_line.shp
> but the output I need is:
> C... ogr2ogr T21-PUENTESshp -where "LAYER=' T21-PUENTES' "
> tapalpa_05_plani_line.shp
>
> I did the Antonio's suggested corrections, and I got the string I wanted,
> now the problem is that my subprocess doesn't work properly, I'll give a
> look and see whats wrong with it.
>
>
> 2010/10/7 taserian <taserian at gmail.com>
>
>  On Thu, Oct 7, 2010 at 12:48 PM, taserian <taserian at gmail.com> wrote:
>>
>>> I'm adding some line breaks to make your text a little more readable.
>>>
>>> On Thu, Oct 7, 2010 at 9:55 AM, Susana Iraiis Delgado Rodriguez <
>>> susana.delgado_s at utzmg.edu.mx> wrote:
>>>
>>>  Hello members:
>>>>
>>>> How can I write a statement to execute the following:
>>>>
>>>
>>>
>>>> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr R1G-GEODESIA.shp -where
>>>> "LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf
>>>>
>>>
>>>
>>>> I want my uotput to look like this.
>>>> Instead I'm getting this
>>>>
>>>
>>>
>>>> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr T21-PUENTES.shp -where
>>>> LAYER=+line tapalpa_05_plani_line.shp
>>>>
>>>
>>>
>>>> In miy code line is a string given by the user:
>>>>
>>>> for line in open("unico.txt", "r").readlines():
>>>>      p = subprocess.Popen(['C:/Archivos de
>>>> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "LAYER='line'",
>>>> b+'.shp'])
>>>>
>>>> Any suggestions?
>>>>
>>>
>>> Without knowing specifics about what the subprocess.Popen function is
>>> expecting as parameters, I can only speculate, but it seems that the
>>> following *might* work (for extremely generous values of "*might*"):
>>>
>>>  for line in open("unico.txt", "r").readlines():
>>>      p = subprocess.Popen(['C:/Archivos de
>>> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "\"LAYER='" +
>>> line + "'\"", b+'.shp'])
>>>
>>> Details about where the changes are:
>>> "\"LAYER='" + line + "'\""
>>>
>>
>> Begin corrections (corrections start with a *)
>>
>>   Quote to begin the literal: "
>>> An escaped quote (1) so that there's a quote inside the literal: \"
>>> Some of the text that's meant to be unchanging: LAYER=
>>>
>> *Single Quote (2) to be included in the literal (which doesn't need to be
>> escaped): '
>>
>>>  Close Quote: "
>>>
>>  Add the content of the variable "line" from the unico.txt file:  + line
>>> +
>>>
>> *Add another literal, composed of the single quote that closes (2) above,
>> then the closing escaped quote to close (1) : "'\""
>>
>>
>>>
>>> See if this works, and let us know how it turns out.
>>>
>>> Antonio Rodriguez
>>>
>>
>> End of corrections.
>>
>> Antonio Rodriguez
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101008/644cd26c/attachment-0001.html>


More information about the Tutor mailing list