[Tutor] PYTHON QUOTES ISSUE

taserian taserian at gmail.com
Fri Oct 8 18:05:47 CEST 2010


On Fri, Oct 8, 2010 at 10:27 AM, Susana Iraiis Delgado Rodriguez <
susana.delgado_s at utzmg.edu.mx> wrote:

> 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:
>

<snip />


> ##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()
>

I don't see how this is eliminating duplicate lines. To me it looks like
it's just copying the contents of capas.txt into unico.txt and adding extra
\n (line breaks), which might be what's breaking your next line.


>
> ##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?
>

Also, in your next e-mail, I noticed you're getting carriage returns (line
breaks in your output). Since you're adding those line breaks above, you
want to remove them before using them somewhere else.

p = subprocess.Popen(['C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr',
line.replace("\n", "") +'.shp', '-where', "\"LAYER='"+ line.replace("\n",
"") +"'\"" , b+'.shp'])


In the code above, I've changed the references to the variable *line* so
that the \n's are replaced by 0-length strings. That should remove them from
the Popen arguments.

Antonio Rodriguez
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101008/cf6393b1/attachment.html>


More information about the Tutor mailing list