Re: [Tutor] PYTHON QUOTES ISSUE

2010-10-08 Thread taserian
On Fri, Oct 8, 2010 at 10:27 AM, Susana Iraiis Delgado Rodriguez <
susana.delgad...@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:
>




> ##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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PYTHON QUOTES ISSUE

2010-10-08 Thread Susana Iraiis Delgado Rodriguez
Besides, If I print the query, I have the next output:

C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr U3B-BARDA

.shp -where "LAYER='U3B-BARDA

'" tapalpa_05_plani_line.shp

C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr N2H-TEMP

.shp -where "LAYER='N2H-TEMP

'" tapalpa_05_plani_line.shp

C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr U3C-CERCA

.shp -where "LAYER='U3C-CERCA

'" tapalpa_05_plani_line.shp..
I thought it was a console print issue, but when I run the module from
Eclipse, it shows the same enter space, so I think it maybe that the line is
not together as it is a statement!!
2010/10/8 Susana Iraiis Delgado Rodriguez 

> 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 
>
>  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 
>>
>>  On Thu, Oct 7, 2010 at 12:48 PM, taserian  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.delgad...@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
>>>

Re: [Tutor] PYTHON QUOTES ISSUE

2010-10-08 Thread Susana Iraiis Delgado Rodriguez
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 

> 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 
>
>  On Thu, Oct 7, 2010 at 12:48 PM, taserian  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.delgad...@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
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PYTHON QUOTES ISSUE

2010-10-07 Thread Alan Gauld


"Susana Iraiis Delgado Rodriguez"  
wrote



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.


erm, like what? The command string above?
So where is this output coming from?
Or are you talking about the output from the command?


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


The difference is that you seem to be getting +line as a literal
string instead of the variables value. Is that the problem?



In miy code line is a string given by the user:

for line in open("unico.txt", "r").readlines():


You donlt need the readlines you can iterate over the open
file.



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

b+'.shp'])


Notice that you have quotes around the line following LAYER=


Any suggestions?


remove the quotes? And shift the double quotes after the = sign.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PYTHON QUOTES ISSUE

2010-10-07 Thread Susana Iraiis Delgado Rodriguez
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 

>  On Thu, Oct 7, 2010 at 12:48 PM, taserian  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.delgad...@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
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PYTHON QUOTES ISSUE

2010-10-07 Thread taserian
On Thu, Oct 7, 2010 at 12:48 PM, taserian  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.delgad...@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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PYTHON QUOTES ISSUE

2010-10-07 Thread taserian
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.delgad...@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 + "'\""

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="
Close Quote: "
Add the content of the variable "line" from the unico.txt file:  + line +
Add another literal, composed of just the closing escaped quote to close the
quote above (1) : "\""

See if this works, and let us know how it turns out.

Antonio Rodriguez
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor