Re: Understanding how to quote XML string in order to serialize using Python's ElementTree

2016-01-09 Thread Karim


Yes it changes your quotechar = "'" into quotechar = '"'

You should no more get the double quoting of the data string and no more 
slicing step.


Karim

On 10/01/2016 00:15, Saran Ahluwalia wrote:
Thank you for the feedback on this. I believe that the excel dialect 
includes just that:


class excel(Dialect):
 delimiter = ','
 quotechar = '"'
 doublequote = True
 skipinitialspace = False
 lineterminator = '\r\n'
 quoting = QUOTE_MINIMAL

On Sat, Jan 9, 2016 at 5:23 PM, Karim <mailto:kliat...@gmail.com>> wrote:




On 09/01/2016 21:54, kbtyo wrote:

My specs:

Python 3.4.3
Windows 7
IDE is Jupyter Notebooks

What I have referenced:

1)

http://stackoverflow.com/questions/1546717/python-escaping-strings-for-use-in-xml

2)

http://stackoverflow.com/questions/7802418/how-to-properly-escape-single-and-double-quotes


3)http://stackoverflow.com/questions/4972210/escaping-characters-in-a-xml-file-with-python


Here is the data (in CSV format) and script, respectively, (I
have tried variations on serializing Column 'E' using both Sax
and ElementTree):

i)

A,B,C,D,E,F,G,H,I,J
"3","8","1","2312285SChecking10","',0001,0070,","1967-12-25
22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0"

ii)

#!/usr/bin/python
# -*-  coding: utf-8 -*-
import os.path
import sys
import csv
from io import StringIO
import xml.etree.cElementTree as ElementTree
from xml.etree.ElementTree import XMLParser
import xml
import xml.sax
from xml.sax import ContentHandler

class MyHandler(xml.sax.handler.ContentHandler):
 def __init__(self):
 self._charBuffer = []
 self._result = []

 def _getCharacterData(self):
 data = ''.join(self._charBuffer).strip()
 self._charBuffer = []
 return data.strip() #remove strip() if whitespace is
important

 def parse(self, f):
 xml.sax.parse(f, self)
 return self._result

 def characters(self, data):
 self._charBuffer.append(data)

 def startElement(self, name, attrs):
 if name == 'Response':
 self._result.append({})

 def endElement(self, name):
 if not name == 'Response': self._result[-1][name] =
self._getCharacterData()

def read_data(path):
 with open(path, 'rU', encoding='utf-8') as data:
 reader = csv.DictReader(data, delimiter =',',
quotechar="'", skipinitialspace=True)
 for row in reader:
 yield row

if __name__ == "__main__":
 empty = ''
 Response = 'sample.csv'
 for idx, row in enumerate(read_data(Response)):
 if idx > 10: break
 data = row['E']
 print(data) # The before
 data = data[1:-1]
 data = ""'{}'"".format(data)
 print(data) # Sanity check
# data = '',0001,0070,'
 try:
 root = ElementTree.XML(data)
# print(root)
 except StopIteration:
 raise
 pass
# xmlstring = StringIO(data)
# print(xmlstring)
# Handler = MyHandler().parse(xmlstring)


Specifically, due to the quoting in the CSV file (which is
beyond my control), I have had to resort to slicing the string
(line 51) and then formatting it (line 52).

However the print out from the above attempt is as follows:

"'


   File "", line unknown
ParseError: no element found: line 1, column 69
Interestingly - if I assign the variable "data" (as in line
54) I receive this:

   File "", line 56
data = '',0001,0070,'
  ^
SyntaxError: invalid token

I seek feedback and information on how to address utilizing
the most Pythonic means to do so. Ideally, is there a method
that can leverage ElementTree. Thank you, in advance, for your
feedback and guidance.


In  fact to get rid of double quote simply create your csv reader
like that:

reader = csv.DictReader(data, dialect='excel', skipinitialspace=True)

You should then don't need to slice data variable and reformat it.

Karim





--
https://mail.python.org/mailman/listinfo/python-list


Re: Understanding how to quote XML string in order to serialize using Python's ElementTree

2016-01-09 Thread Saran Ahluwalia
Thank you for the feedback on this. I believe that the excel dialect
includes just that:

class excel(Dialect):
delimiter = ','
quotechar = '"'
doublequote = True
skipinitialspace = False
lineterminator = '\r\n'
quoting = QUOTE_MINIMAL


On Sat, Jan 9, 2016 at 5:23 PM, Karim  wrote:

>
>
> On 09/01/2016 21:54, kbtyo wrote:
>
>> My specs:
>>
>> Python 3.4.3
>> Windows 7
>> IDE is Jupyter Notebooks
>>
>> What I have referenced:
>>
>> 1)
>> http://stackoverflow.com/questions/1546717/python-escaping-strings-for-use-in-xml
>>
>> 2)
>>
>> http://stackoverflow.com/questions/7802418/how-to-properly-escape-single-and-double-quotes
>>
>> 3)
>> http://stackoverflow.com/questions/4972210/escaping-characters-in-a-xml-file-with-python
>>
>>
>> Here is the data (in CSV format) and script, respectively, (I have tried
>> variations on serializing Column 'E' using both Sax and ElementTree):
>>
>> i)
>>
>> A,B,C,D,E,F,G,H,I,J
>> "3","8","1","> />2312> />285SChecking10","> TransactionID="2"
>> RequestType="HoldInquiry">',0001,0070,","1967-12-25
>> 22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0"
>>
>> ii)
>>
>> #!/usr/bin/python
>> # -*-  coding: utf-8 -*-
>> import os.path
>> import sys
>> import csv
>> from io import StringIO
>> import xml.etree.cElementTree as ElementTree
>> from xml.etree.ElementTree import XMLParser
>> import xml
>> import xml.sax
>> from xml.sax import ContentHandler
>>
>> class MyHandler(xml.sax.handler.ContentHandler):
>>  def __init__(self):
>>  self._charBuffer = []
>>  self._result = []
>>
>>  def _getCharacterData(self):
>>  data = ''.join(self._charBuffer).strip()
>>  self._charBuffer = []
>>  return data.strip() #remove strip() if whitespace is important
>>
>>  def parse(self, f):
>>  xml.sax.parse(f, self)
>>  return self._result
>>
>>  def characters(self, data):
>>  self._charBuffer.append(data)
>>
>>  def startElement(self, name, attrs):
>>  if name == 'Response':
>>  self._result.append({})
>>
>>  def endElement(self, name):
>>  if not name == 'Response': self._result[-1][name] =
>> self._getCharacterData()
>>
>> def read_data(path):
>>  with open(path, 'rU', encoding='utf-8') as data:
>>  reader = csv.DictReader(data, delimiter =',', quotechar="'",
>> skipinitialspace=True)
>>  for row in reader:
>>  yield row
>>
>> if __name__ == "__main__":
>>  empty = ''
>>  Response = 'sample.csv'
>>  for idx, row in enumerate(read_data(Response)):
>>  if idx > 10: break
>>  data = row['E']
>>  print(data) # The before
>>  data = data[1:-1]
>>  data = ""'{}'"".format(data)
>>  print(data) # Sanity check
>> # data = '> RequestType="HoldInquiry">',0001,0070,'
>>  try:
>>  root = ElementTree.XML(data)
>> # print(root)
>>  except StopIteration:
>>  raise
>>  pass
>> # xmlstring = StringIO(data)
>> # print(xmlstring)
>> # Handler = MyHandler().parse(xmlstring)
>>
>>
>> Specifically, due to the quoting in the CSV file (which is beyond my
>> control), I have had to resort to slicing the string (line 51) and then
>> formatting it (line 52).
>>
>> However the print out from the above attempt is as follows:
>>
>> "'
>> 
>>
>>File "", line unknown
>> ParseError: no element found: line 1, column 69
>> Interestingly - if I assign the variable "data" (as in line 54) I receive
>> this:
>>
>>File "", line 56
>> data = '> RequestType="HoldInquiry">',0001,0070,'
>>   ^
>> SyntaxError: invalid token
>>
>> I seek feedback and information on how to address utilizing the most
>> Pythonic means to do so. Ideally, is there a method that can leverage
>> ElementTree. Thank you, in advance, for your feedback and guidance.
>>
>
> In  fact to get rid of double quote simply create your csv reader like
> that:
>
> reader = csv.DictReader(data, dialect='excel', skipinitialspace=True)
>
> You should then don't need to slice data variable and reformat it.
>
> Karim
>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Understanding how to quote XML string in order to serialize using Python's ElementTree

2016-01-09 Thread Saran Ahluwalia
As mentioned previously, I assigned the variable *data *to the string
''',0001,0070,". When any utility that
attempts to parse this string (this is one example of millions found in my
actual data source) you receive that error. You would need to comment out
the following:

data = row['E']
print(data) # The before
data = data[1:-1]
data = ""'{}'"".format(data)
print(data) # Sanity check

to achieve the readout.

Unfortunately, I am wondering if there is a scalable solution to the above
issue (perhaps using some form of escape character or regex?). I have ideas
and have tried many but to no avail. There always seems to be an edge case
that escapes me. Thanks.

On Sat, Jan 9, 2016 at 5:08 PM, Karim  wrote:

>
>
> On 09/01/2016 21:54, kbtyo wrote:
>
>> My specs:
>>
>> Python 3.4.3
>> Windows 7
>> IDE is Jupyter Notebooks
>>
>> What I have referenced:
>>
>> 1)
>> http://stackoverflow.com/questions/1546717/python-escaping-strings-for-use-in-xml
>>
>> 2)
>>
>> http://stackoverflow.com/questions/7802418/how-to-properly-escape-single-and-double-quotes
>>
>> 3)
>> http://stackoverflow.com/questions/4972210/escaping-characters-in-a-xml-file-with-python
>>
>>
>> Here is the data (in CSV format) and script, respectively, (I have tried
>> variations on serializing Column 'E' using both Sax and ElementTree):
>>
>> i)
>>
>> A,B,C,D,E,F,G,H,I,J
>> "3","8","1","> />2312> />285SChecking10","> TransactionID="2"
>> RequestType="HoldInquiry">',0001,0070,","1967-12-25
>> 22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0"
>>
>> ii)
>>
>> #!/usr/bin/python
>> # -*-  coding: utf-8 -*-
>> import os.path
>> import sys
>> import csv
>> from io import StringIO
>> import xml.etree.cElementTree as ElementTree
>> from xml.etree.ElementTree import XMLParser
>> import xml
>> import xml.sax
>> from xml.sax import ContentHandler
>>
>> class MyHandler(xml.sax.handler.ContentHandler):
>>  def __init__(self):
>>  self._charBuffer = []
>>  self._result = []
>>
>>  def _getCharacterData(self):
>>  data = ''.join(self._charBuffer).strip()
>>  self._charBuffer = []
>>  return data.strip() #remove strip() if whitespace is important
>>
>>  def parse(self, f):
>>  xml.sax.parse(f, self)
>>  return self._result
>>
>>  def characters(self, data):
>>  self._charBuffer.append(data)
>>
>>  def startElement(self, name, attrs):
>>  if name == 'Response':
>>  self._result.append({})
>>
>>  def endElement(self, name):
>>  if not name == 'Response': self._result[-1][name] =
>> self._getCharacterData()
>>
>> def read_data(path):
>>  with open(path, 'rU', encoding='utf-8') as data:
>>  reader = csv.DictReader(data, delimiter =',', quotechar="'",
>> skipinitialspace=True)
>>  for row in reader:
>>  yield row
>>
>> if __name__ == "__main__":
>>  empty = ''
>>  Response = 'sample.csv'
>>  for idx, row in enumerate(read_data(Response)):
>>  if idx > 10: break
>>  data = row['E']
>>  print(data) # The before
>>  data = data[1:-1]
>>  data = ""'{}'"".format(data)
>>  print(data) # Sanity check
>> # data = '> RequestType="HoldInquiry">',0001,0070,'
>>  try:
>>  root = ElementTree.XML(data)
>> # print(root)
>>  except StopIteration:
>>  raise
>>  pass
>> # xmlstring = StringIO(data)
>> # print(xmlstring)
>> # Handler = MyHandler().parse(xmlstring)
>>
>>
>> Specifically, due to the quoting in the CSV file (which is beyond my
>> control), I have had to resort to slicing the string (line 51) and then
>> formatting it (line 52).
>>
>> However the print out from the above attempt is as follows:
>>
>> "'
>> 
>>
>>File "", line unknown
>> ParseError: no element found: line 1, column 69
>> Interestingly - if I assign the variable "data" (as in line 54) I receive
>> this:
>>
>>File "", line 56
>> data = '> RequestType="HoldInquiry">',0001,0070,'
>>   ^
>> SyntaxError: invalid token
>>
>> I seek feedback and information on how to address utilizing the most
>> Pythonic means to do so. Ideally, is there a method that can leverage
>> ElementTree. Thank you, in advance, for your feedback and guidance.
>>
>
> I don't understand because this line 54 gives:
>
> >>> import xml.etree.cElementTree as ElementTree
> >>> data = ' RequestType="HoldInquiry">',0001,0070,'
>   File "", line 1
> data = ' RequestType="HoldInquiry">',0001,0070,'
> ^
> SyntaxError: invalid syntax
>
>
> BUT IF you correct the string and remove the inner quote after 
> everything's fine:
> >>> data = ' RequestType="HoldInquiry">,0001,0070,
> '
> >>> root = ElementTree.XML(data)
> >>> root
> 
> Karim
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Understanding how to quote XML string in order to serialize using Python's ElementTree

2016-01-09 Thread Karim



On 09/01/2016 21:54, kbtyo wrote:

My specs:

Python 3.4.3
Windows 7
IDE is Jupyter Notebooks

What I have referenced:

1) 
http://stackoverflow.com/questions/1546717/python-escaping-strings-for-use-in-xml

2)
http://stackoverflow.com/questions/7802418/how-to-properly-escape-single-and-double-quotes

3)http://stackoverflow.com/questions/4972210/escaping-characters-in-a-xml-file-with-python


Here is the data (in CSV format) and script, respectively, (I have tried 
variations on serializing Column 'E' using both Sax and ElementTree):

i)

A,B,C,D,E,F,G,H,I,J
"3","8","1","2312285SChecking10","',0001,0070,","1967-12-25 22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0"

ii)

#!/usr/bin/python
# -*-  coding: utf-8 -*-
import os.path
import sys
import csv
from io import StringIO
import xml.etree.cElementTree as ElementTree
from xml.etree.ElementTree import XMLParser
import xml
import xml.sax
from xml.sax import ContentHandler

class MyHandler(xml.sax.handler.ContentHandler):
 def __init__(self):
 self._charBuffer = []
 self._result = []

 def _getCharacterData(self):
 data = ''.join(self._charBuffer).strip()
 self._charBuffer = []
 return data.strip() #remove strip() if whitespace is important

 def parse(self, f):
 xml.sax.parse(f, self)
 return self._result
 


 def characters(self, data):
 self._charBuffer.append(data)

 def startElement(self, name, attrs):
 if name == 'Response':
 self._result.append({})

 def endElement(self, name):
 if not name == 'Response': self._result[-1][name] = 
self._getCharacterData()

def read_data(path):
 with open(path, 'rU', encoding='utf-8') as data:
 reader = csv.DictReader(data, delimiter =',', quotechar="'", 
skipinitialspace=True)
 for row in reader:
 yield row

if __name__ == "__main__":
 empty = ''
 Response = 'sample.csv'
 for idx, row in enumerate(read_data(Response)):
 if idx > 10: break
 data = row['E']
 print(data) # The before
 data = data[1:-1]
 data = ""'{}'"".format(data)
 print(data) # Sanity check
# data = '',0001,0070,'
 try:
 root = ElementTree.XML(data)
# print(root)
 except StopIteration:
 raise
 pass
# xmlstring = StringIO(data)
# print(xmlstring)
# Handler = MyHandler().parse(xmlstring)


Specifically, due to the quoting in the CSV file (which is beyond my control), 
I have had to resort to slicing the string (line 51) and then formatting it 
(line 52).

However the print out from the above attempt is as follows:

"'


   File "", line unknown
ParseError: no element found: line 1, column 69
Interestingly - if I assign the variable "data" (as in line 54) I receive this:

   File "", line 56
data = '',0001,0070,'
  ^
SyntaxError: invalid token

I seek feedback and information on how to address utilizing the most Pythonic 
means to do so. Ideally, is there a method that can leverage ElementTree. Thank 
you, in advance, for your feedback and guidance.


In  fact to get rid of double quote simply create your csv reader like that:

reader = csv.DictReader(data, dialect='excel', skipinitialspace=True)

You should then don't need to slice data variable and reformat it.

Karim


--
https://mail.python.org/mailman/listinfo/python-list


Re: Understanding how to quote XML string in order to serialize using Python's ElementTree

2016-01-09 Thread Karim



On 09/01/2016 21:54, kbtyo wrote:

My specs:

Python 3.4.3
Windows 7
IDE is Jupyter Notebooks

What I have referenced:

1) 
http://stackoverflow.com/questions/1546717/python-escaping-strings-for-use-in-xml

2)
http://stackoverflow.com/questions/7802418/how-to-properly-escape-single-and-double-quotes

3)http://stackoverflow.com/questions/4972210/escaping-characters-in-a-xml-file-with-python


Here is the data (in CSV format) and script, respectively, (I have tried 
variations on serializing Column 'E' using both Sax and ElementTree):

i)

A,B,C,D,E,F,G,H,I,J
"3","8","1","2312285SChecking10","',0001,0070,","1967-12-25 22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0"

ii)

#!/usr/bin/python
# -*-  coding: utf-8 -*-
import os.path
import sys
import csv
from io import StringIO
import xml.etree.cElementTree as ElementTree
from xml.etree.ElementTree import XMLParser
import xml
import xml.sax
from xml.sax import ContentHandler

class MyHandler(xml.sax.handler.ContentHandler):
 def __init__(self):
 self._charBuffer = []
 self._result = []

 def _getCharacterData(self):
 data = ''.join(self._charBuffer).strip()
 self._charBuffer = []
 return data.strip() #remove strip() if whitespace is important

 def parse(self, f):
 xml.sax.parse(f, self)
 return self._result
 


 def characters(self, data):
 self._charBuffer.append(data)

 def startElement(self, name, attrs):
 if name == 'Response':
 self._result.append({})

 def endElement(self, name):
 if not name == 'Response': self._result[-1][name] = 
self._getCharacterData()

def read_data(path):
 with open(path, 'rU', encoding='utf-8') as data:
 reader = csv.DictReader(data, delimiter =',', quotechar="'", 
skipinitialspace=True)
 for row in reader:
 yield row

if __name__ == "__main__":
 empty = ''
 Response = 'sample.csv'
 for idx, row in enumerate(read_data(Response)):
 if idx > 10: break
 data = row['E']
 print(data) # The before
 data = data[1:-1]
 data = ""'{}'"".format(data)
 print(data) # Sanity check
# data = '',0001,0070,'
 try:
 root = ElementTree.XML(data)
# print(root)
 except StopIteration:
 raise
 pass
# xmlstring = StringIO(data)
# print(xmlstring)
# Handler = MyHandler().parse(xmlstring)


Specifically, due to the quoting in the CSV file (which is beyond my control), 
I have had to resort to slicing the string (line 51) and then formatting it 
(line 52).

However the print out from the above attempt is as follows:

"'


   File "", line unknown
ParseError: no element found: line 1, column 69
Interestingly - if I assign the variable "data" (as in line 54) I receive this:

   File "", line 56
data = '',0001,0070,'
      ^
SyntaxError: invalid token

I seek feedback and information on how to address utilizing the most Pythonic 
means to do so. Ideally, is there a method that can leverage ElementTree. Thank 
you, in advance, for your feedback and guidance.


I don't understand because this line 54 gives:

>>> import xml.etree.cElementTree as ElementTree
>>> data = 'RequestType="HoldInquiry">',0001,0070,'

  File "", line 1
data = 'RequestType="HoldInquiry">',0001,0070,'

^
SyntaxError: invalid syntax


BUT IF you correct the string and remove the inner quote after  
everything's fine:
>>> data = 'RequestType="HoldInquiry">,0001,0070, 
'

>>> root = ElementTree.XML(data)
>>> root

Karim

--
https://mail.python.org/mailman/listinfo/python-list


Understanding how to quote XML string in order to serialize using Python's ElementTree

2016-01-09 Thread kbtyo
My specs:

Python 3.4.3
Windows 7
IDE is Jupyter Notebooks

What I have referenced:

1) 
http://stackoverflow.com/questions/1546717/python-escaping-strings-for-use-in-xml

2)
http://stackoverflow.com/questions/7802418/how-to-properly-escape-single-and-double-quotes

3)http://stackoverflow.com/questions/4972210/escaping-characters-in-a-xml-file-with-python


Here is the data (in CSV format) and script, respectively, (I have tried 
variations on serializing Column 'E' using both Sax and ElementTree):

i)

A,B,C,D,E,F,G,H,I,J
"3","8","1","2312285SChecking10","',0001,0070,","1967-12-25
 22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0"

ii)

#!/usr/bin/python
# -*-  coding: utf-8 -*-
import os.path
import sys
import csv
from io import StringIO 
import xml.etree.cElementTree as ElementTree
from xml.etree.ElementTree import XMLParser
import xml
import xml.sax
from xml.sax import ContentHandler

class MyHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self._charBuffer = []
self._result = []

def _getCharacterData(self):
data = ''.join(self._charBuffer).strip()
self._charBuffer = []
return data.strip() #remove strip() if whitespace is important

def parse(self, f):
xml.sax.parse(f, self)
return self._result


def characters(self, data):
self._charBuffer.append(data)

def startElement(self, name, attrs):
if name == 'Response':
self._result.append({})

def endElement(self, name):
if not name == 'Response': self._result[-1][name] = 
self._getCharacterData()

def read_data(path):
with open(path, 'rU', encoding='utf-8') as data:
reader = csv.DictReader(data, delimiter =',', quotechar="'", 
skipinitialspace=True)
for row in reader:
yield row

if __name__ == "__main__":
empty = ''
Response = 'sample.csv'
for idx, row in enumerate(read_data(Response)):
if idx > 10: break
data = row['E']
print(data) # The before
data = data[1:-1]
data = ""'{}'"".format(data)
print(data) # Sanity check 
# data = '',0001,0070,'
try:
root = ElementTree.XML(data)
# print(root)
except StopIteration:
raise
pass
# xmlstring = StringIO(data)
# print(xmlstring)
# Handler = MyHandler().parse(xmlstring)


Specifically, due to the quoting in the CSV file (which is beyond my control), 
I have had to resort to slicing the string (line 51) and then formatting it 
(line 52).

However the print out from the above attempt is as follows:

"'


  File "", line unknown
ParseError: no element found: line 1, column 69
Interestingly - if I assign the variable "data" (as in line 54) I receive this:

  File "", line 56
data = '',0001,0070,'
 ^
SyntaxError: invalid token

I seek feedback and information on how to address utilizing the most Pythonic 
means to do so. Ideally, is there a method that can leverage ElementTree. Thank 
you, in advance, for your feedback and guidance.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help with ElementTree

2015-04-09 Thread Larry Martell
On Thu, Apr 9, 2015 at 2:42 PM, Ben Finney  wrote:
> Larry Martell  writes:
>
>> I have an XML file that looks like this (this is just the pertinent
>> part, the file is huge):
>
> It's also not a very helpful schema. Elements called “Node”, where the
> actual type of element is in an attribute, just make your job needlessly
> harder.
>
> They also pointlessly force you to use mixed case, when the convention
> is to name all the elements in lowercase (‘parameter’, ‘current’, etc.).
>
> That said: What you need for this kind of searching within XML documents
> is the XPath query language. ElementTree has limited support for it
> https://docs.python.org/3/library/xml.etree.elementtree.html#elementtree-xpath>,
> enough for your stated requirements.
>
>> I would like to use ElementTree to get 2 values from this:
>
> >>> from xml.etree import ElementTree
> >>> doc_text = """… your example document text here …"""
> >>> doc_tree = ElementTree.fromstring(doc_text)
>
>> SystemConfig.Environment.ToolName.Current
>
> >>> toolname_xpath = 
> ".//Node[@Name='SystemConfig']/Node[@Name='Environment']/Parameter[@Name='ToolName']//Current"
> >>> toolname_element = doc_tree.find(toolname_xpath)
> >>> toolname_element
> 
> >>> toolname_element.text
> 'KA21'
>
>> Events.LastEventExportTime.Current
>
> >>> lasteventexporttime_xpath = 
> ".//Node[@Name='Events']/Parameter[@Name='LastEventExportTime']//Current"
> >>> lasteventexporttime_element =
> >>> doc_tree.find(lasteventexporttime_xpath)
> >>> lasteventexporttime_element
> 
> >>> lasteventexporttime_element.text
> '15/03/2014 05:56:00'


Thanks. I have this working with the help of Peter's reply.

>> I've been trying for hours to get ElementTree to give me these 2
>> values, but nothing I do seems to work. Can anyone help me with this?
>
> Beat the designers of that document upside the head until they give you
> a more easily-parsed schema.

It's programmatically generated by various pieces of semiconductor
manufacturing equipment. It's not in my power to get that changed.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help with ElementTree

2015-04-09 Thread Larry Martell
On Thu, Apr 9, 2015 at 2:39 PM, Peter Otten <__pete...@web.de> wrote:
> Larry Martell wrote:
>
>> I have an XML file that looks like this (this is just the pertinent
>> part, the file is huge):
>>
>> 
>> 
>>   
>> 
>> 
>>
>>   
>>  KA21
>>  KA21
>>  
>>
>>  
>>   
>>
>> 
>>
>>  
>>00:00:00
>>15/03/2014 05:56:00
>>  
>>
>> 
>>   
>> 
>>
>> I would like to use ElementTree to get 2 values from this:
>>
>> SystemConfig.Environment.ToolName.Current
>> Events.LastEventExportTime.Current
>>
>> I've been trying for hours to get ElementTree to give me these 2
>> values, but nothing I do seems to work. Can anyone help me with this?
>
> Try it in the interactive interpreter, one step after another:
>
>>>> from xml.etree import ElementTree
>>>> root = ElementTree.fromstring("""
> ... 
> 
> ... 
> ... """)
>>>> root.find("Doc")
> 
>>>> root.find("Doc/Node")
> 
>>>> root.find("Doc/Node/Node/Parameter/Value/Current").text
> 'KA21'
>
> That "worked" because the Node elements involved are the first in the
> document. You may have to add more "conditions" until there is no ambiguity
> left, e. g. to pick the child node of Doc with the Name="Events" attribute:
>
>>>> root.find("Doc/Node[@Name='Events']/Parameter/Value/Current").text
> '15/03/2014 05:56:00'
>
> See
>
> https://docs.python.org/dev/library/xml.etree.elementtree.html#xpath-support
>
> for the details.

Thanks! I did try in the interpreter and I did look at the doc you
referenced. I just couldn't get it to do what I wanted. I understand
this a lot better now. My example XML was grossly simplified, but I
can get what I need with this syntax:

root.find("Doc/Node[@Name='Events']/Parameter[@Name='LastEventExportTime']/Value/Current").text
root.find("Doc/Node[@Name='SystemConfig']/Node[@Name='Environment']/Parameter[@Name='ToolName']/Value/Current").text
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help with ElementTree

2015-04-09 Thread Ben Finney
Larry Martell  writes:

> I have an XML file that looks like this (this is just the pertinent
> part, the file is huge):

It's also not a very helpful schema. Elements called “Node”, where the
actual type of element is in an attribute, just make your job needlessly
harder.

They also pointlessly force you to use mixed case, when the convention
is to name all the elements in lowercase (‘parameter’, ‘current’, etc.).

That said: What you need for this kind of searching within XML documents
is the XPath query language. ElementTree has limited support for it
https://docs.python.org/3/library/xml.etree.elementtree.html#elementtree-xpath>,
enough for your stated requirements.

> I would like to use ElementTree to get 2 values from this:

>>> from xml.etree import ElementTree
>>> doc_text = """… your example document text here …"""
>>> doc_tree = ElementTree.fromstring(doc_text)

> SystemConfig.Environment.ToolName.Current

>>> toolname_xpath = 
".//Node[@Name='SystemConfig']/Node[@Name='Environment']/Parameter[@Name='ToolName']//Current"
>>> toolname_element = doc_tree.find(toolname_xpath)
>>> toolname_element

>>> toolname_element.text
'KA21'

> Events.LastEventExportTime.Current

>>> lasteventexporttime_xpath = 
".//Node[@Name='Events']/Parameter[@Name='LastEventExportTime']//Current"
>>> lasteventexporttime_element =
>>> doc_tree.find(lasteventexporttime_xpath)
>>> lasteventexporttime_element

>>> lasteventexporttime_element.text
'15/03/2014 05:56:00'

> I've been trying for hours to get ElementTree to give me these 2
> values, but nothing I do seems to work. Can anyone help me with this?

Beat the designers of that document upside the head until they give you
a more easily-parsed schema.

-- 
 \“Considering the current sad state of our computer programs, |
  `\ software development is clearly still a black art, and cannot |
_o__)  yet be called an engineering discipline.” —Bill Clinton |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help with ElementTree

2015-04-09 Thread Peter Otten
Larry Martell wrote:

> I have an XML file that looks like this (this is just the pertinent
> part, the file is huge):
> 
> 
> 
>   
> 
> 
>
>   
>  KA21
>  KA21
>  
>
>  
>   
> 
> 
>
>  
>00:00:00
>15/03/2014 05:56:00
>      
>
> 
>   
> 
> 
> I would like to use ElementTree to get 2 values from this:
> 
> SystemConfig.Environment.ToolName.Current
> Events.LastEventExportTime.Current
> 
> I've been trying for hours to get ElementTree to give me these 2
> values, but nothing I do seems to work. Can anyone help me with this?

Try it in the interactive interpreter, one step after another:

>>> from xml.etree import ElementTree
>>> root = ElementTree.fromstring("""
... 

... 
... """)
>>> root.find("Doc")

>>> root.find("Doc/Node")

>>> root.find("Doc/Node/Node/Parameter/Value/Current").text
'KA21'

That "worked" because the Node elements involved are the first in the 
document. You may have to add more "conditions" until there is no ambiguity 
left, e. g. to pick the child node of Doc with the Name="Events" attribute:

>>> root.find("Doc/Node[@Name='Events']/Parameter/Value/Current").text
'15/03/2014 05:56:00'

See 

https://docs.python.org/dev/library/xml.etree.elementtree.html#xpath-support

for the details.

-- 
https://mail.python.org/mailman/listinfo/python-list


Help with ElementTree

2015-04-09 Thread Larry Martell
I have an XML file that looks like this (this is just the pertinent
part, the file is huge):



  


   
  
 KA21
 KA21
 
   
 
  


   
 
   00:00:00
   15/03/2014 05:56:00
 
   

  


I would like to use ElementTree to get 2 values from this:

SystemConfig.Environment.ToolName.Current
Events.LastEventExportTime.Current

I've been trying for hours to get ElementTree to give me these 2
values, but nothing I do seems to work. Can anyone help me with this?

Thanks!
-larry
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: insert html into ElementTree without parsing it

2014-03-01 Thread Stefan Behnel
graeme.piete...@gmail.com, 24.02.2014 10:45:
> I am building HTML pages using ElementTree.
> I need to insert chunks of untrusted HTML into the page. I do not need or 
> want to parse this, just insert it at a particular point as is.

How would you want to find out if it can be safely inserted or not without
parsing it?


> The best solutions I can think of are rather ugly ones: manipulating the 
> string created by tostring.
> 
> Is there a nicer way of doing this? Is it possible, for example, to customise 
> how an element is converted to a string representation? I am open to using 
> something else (e.g. lxml) if necessary.

lxml has a tool to discard potentially unsafe content from HTML files:

http://lxml.de/lxmlhtml.html#cleaning-up-html

Stefan


-- 
https://mail.python.org/mailman/listinfo/python-list


insert html into ElementTree without parsing it

2014-02-24 Thread graeme . pietersz
I am building HTML pages using ElementTree.

I need to insert chunks of untrusted HTML into the page. I do not need or want 
to parse this, just insert it at a particular point as is.

The best solutions I can think of are rather ugly ones: manipulating the string 
created by tostring.

Is there a nicer way of doing this? Is it possible, for example, to customise 
how an element is converted to a string representation? I am open to using 
something else (e.g. lxml) if necessary.
-- 
https://mail.python.org/mailman/listinfo/python-list


simple ElementTree based parser that allows entity definition map

2013-12-04 Thread Robin Becker
I'm tasked with writing  a 'simple' ElementTree based parser with support for 
unknown entities eg &foo;.


This code derived from FL's old documentation fails in both python 2 and 3.

import xml.etree.ElementTree as ET
try:
ascii
except:
from future_builtins import ascii

class EchoTarget:
def start(self, tag, attrib):
print("start %s %s"%(tag, ascii(attrib)))
def end(self, tag):
print("end %s"%tag)
def data(self, data):
print("data %s" % ascii(data))
def close(self):
print("close")

def __getattr__(self,a):
print('target attempting to get attribute %s' % a)

target = EchoTarget()
parser = ET.XMLParser(target=target)
parser.entity['foo'] = b'&fum;'
parser.entity['fum'] = b''
print("parser.entity=%s" % ascii(parser.entity))
parser.feed("some text &foo;")
parser.feed("")
parser.close()
########

The entity value doesn't seem to get referenced.



I tried this derived from
http://stackoverflow.com/questions/7237466/python-elementtree-support-for-parsing-unknown-xml-entities


__all__=tuple(filter(None,'''
Xml2TT
EntityMap
'''.split()))
import xml.etree.ElementTree as ET
try:
from StringIO import StringIO
except ImportError:
from io import StringIO

class EntityMap(dict):
def __getitem__(self,key):
try:
r = dict.__getitem__(self,key)
except:
r = '&' + key +';'
return r

class Xml2TT:
'''
create a callable object that can turns xml into a tupletree
if mutable is set to True then it's really a list tree
'''
def __init__(self,mutable=False,entityMap=None):
self._mutable = mutable
self._parser = parser = ET.XMLParser()
parser.parser.UseForeignDTD(True)
parser.entity = self._entityMap = entityMap

def __call__(self,xml):
r = self._mtt(ET.ElementTree().parse(StringIO(xml.strip()), 
parser=self._parser))

return r[0]

def _mtt(self,node):
t = [node.text] if node.text else []
e = t.extend
for c in node:
e(self._mtt(c))
t = (node.tag,node.attrib,t,None)
if self._mutable:
t = list(t)
return [t,node.tail] if node.tail else [t]

if __name__=='__main__':
print(repr(Xml2TT()('a22')))
print(repr(Xml2TT()('a=&=b< >')))
print(repr(Xml2TT(entityMap=EntityMap({'mu': '…','foo': 
'AAA&fum;BBB','fum':'CCC'}))('amp=& moo=&moo; lt=< gt=> mu=μ 
foo=&foo;')))



and it sort of works in python2, fails in python3 with

AttributeError: 'xml.etree.ElementTree.XMLParser' object has no attribute
'parser'

Even in python 2 there's a subtle bug as the output is

('a', {}, ['a', ('b', {}, ['', ('c', {'ca': '123'}, [], None), '22'], 
None)], None)

('a', {}, ['a=&=b< >'], None)
('a', {}, [u'amp=& moo=&moo; lt=< gt=> mu=… foo=AAA&fum;BBB'], None)

ie the result of the &foo; lookup is not re-parsed so &fum; is not translated.

Is there a way to get a simple ElementTree based parser that can do what I want? 
I have several hundred entities and the size of the DTD would probably be larger 
than 99% of the strings I need to parse. I think I can live with the 
non-reparsing of the map output, but can I get Python 3 to do the UseForeignDTD 
thing?

--
Robin Becker

--
https://mail.python.org/mailman/listinfo/python-list


Re: parsing nested unbounded XML fields with ElementTree

2013-11-27 Thread Larry Martell
On Tue, Nov 26, 2013 at 8:20 AM, Stefan Behnel  wrote:
> Larry Martell, 26.11.2013 13:23:
>> On Tue, Nov 26, 2013 at 2:38 AM, Stefan Behnel wrote:
>>> larry.martell...@gmail.com, 25.11.2013 23:22:
 I have an XML file that has an element called "Node". These can be nested 
 to any depth and the depth of the nesting is not known to me. I need to 
 parse the file and preserve the nesting. For exmaple, if the XML file had:

 

   
 
   

 When I'm parsing Node "E" I need to know I'm in A/B/C/D/E. Problem is I 
 don't know how deep this can be. This is the code I have so far:

 nodes = []

 def parseChild(c):
 if c.tag == 'Node':
 if 'Name' in c.attrib:
 nodes.append(c.attrib['Name'])
 for c1 in c:
 parseChild(c1)
 else:
 for node in nodes:
 print node,
 print c.tag

 for parent in tree.getiterator():
 for child in parent:
 for x in child:
 parseChild(x)
>>>
>>> This seems hugely redundant. tree.getiterator() already returns a recursive
>>> iterable, and then, for each nodes in your document, you are running
>>> recursively over its entire subtree. Meaning that you'll visit each node as
>>> many times as its depth in the tree.
>>>
>>>
 My problem is that I don't know when I'm done with a node and I should
 remove a level of nesting. I would think this is a fairly common
 situation, but I could not find any examples of parsing a file like
 this. Perhaps I'm going about it completely wrong.
>>>
>>> Your recursive traversal function tells you when you're done. If you drop
>>> the getiterator() bit, reaching the end of parseChild() means that you're
>>> done with the element and start backing up. So you can simply pass down a
>>> list of element names that you append() at the beginning of the function
>>> and pop() at the end, i.e. a stack. That list will then always give you the
>>> current path from the root node.
>>
>> Thanks for the reply. How can I remove getiterator()? Then I won't be
>> traversing the nodes of the tree. I can't iterate over tree. I am also
>> unclear on where to do the pop(). I tried putting it just after the
>> recursive call to parseChild() and I tried putting as the very last
>> statement in parseChild() - neither one gave the desired result. Can
>> you show me in code what you mean?
>
> untested:
>
>   nodes = []
>
>   def process_subtree(c, path):
>   name = c.get('Name') if c.tag == 'Node' else None
>   if name:
>   path.append(name)
>   nodes.append('/'.join(path))
>
>   for c1 in c:
>   process_subtree(c1, path)
>
>   if name:
>   path.pop()
>
>   process_subtree(tree.getroot(), [])

Thanks! This was extremely helpful and I've use these concepts to
write script that successfully parses my file.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parsing nested unbounded XML fields with ElementTree

2013-11-26 Thread Neil Cerutti
On Mon, Nov 25, 2013 at 5:22 PM, larry.mart...@gmail.com
 wrote:
> I have an XML file that has an element called "Node". These can
> be nested to any depth and the depth of the nesting is not
> known to me. I need to parse the file and preserve the nesting.
> For exmaple, if the XML file had:
>
> 
>
>   
> 
>   
>
> When I'm parsing Node "E" I need to know I'm in A/B/C/D/E.
> Problem is I don't know how deep this can be. This is the code
> I have so far:

I also an ElementTree user, but it's fairly heavy-duty for simple
jobs. I use sax for simple those. In fact, I'm kind of a saxophone.
This is basically the same idea as others have posted.

the_xml = """

   
  

  
  """
import io
import sys
import xml.sax as sax


class NodeHandler(sax.handler.ContentHandler):
def startDocument(self):
self.title = ''
self.names = []

def startElement(self, name, attrs):
self.process(attrs['Name'])
self.names.append(attrs['Name'])

def process(self, name):
print("Node {} Nest {}".format(name, '/'.join(self.names)))
# Do your stuff.

def endElement(self, name):
self.names.pop()


print(sys.version_info)
handler = NodeHandler()
parser = sax.parse(io.StringIO(the_xml), handler)

Output:
sys.version_info(major=3, minor=3, micro=2, releaselevel='final', serial=0)
Node A Nest
Node B Nest A
Node C Nest A/B
Node D Nest A/B/C
Node E Nest A/B/C/D

-- 
Neil Cerutti
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parsing nested unbounded XML fields with ElementTree

2013-11-26 Thread Stefan Behnel
Larry Martell, 26.11.2013 13:23:
> On Tue, Nov 26, 2013 at 2:38 AM, Stefan Behnel wrote:
>> larry.martell...@gmail.com, 25.11.2013 23:22:
>>> I have an XML file that has an element called "Node". These can be nested 
>>> to any depth and the depth of the nesting is not known to me. I need to 
>>> parse the file and preserve the nesting. For exmaple, if the XML file had:
>>>
>>> 
>>>
>>>   
>>> 
>>>   
>>>
>>> When I'm parsing Node "E" I need to know I'm in A/B/C/D/E. Problem is I 
>>> don't know how deep this can be. This is the code I have so far:
>>>
>>> nodes = []
>>>
>>> def parseChild(c):
>>> if c.tag == 'Node':
>>> if 'Name' in c.attrib:
>>> nodes.append(c.attrib['Name'])
>>> for c1 in c:
>>> parseChild(c1)
>>> else:
>>> for node in nodes:
>>> print node,
>>> print c.tag
>>>
>>> for parent in tree.getiterator():
>>> for child in parent:
>>> for x in child:
>>> parseChild(x)
>>
>> This seems hugely redundant. tree.getiterator() already returns a recursive
>> iterable, and then, for each nodes in your document, you are running
>> recursively over its entire subtree. Meaning that you'll visit each node as
>> many times as its depth in the tree.
>>
>>
>>> My problem is that I don't know when I'm done with a node and I should
>>> remove a level of nesting. I would think this is a fairly common
>>> situation, but I could not find any examples of parsing a file like
>>> this. Perhaps I'm going about it completely wrong.
>>
>> Your recursive traversal function tells you when you're done. If you drop
>> the getiterator() bit, reaching the end of parseChild() means that you're
>> done with the element and start backing up. So you can simply pass down a
>> list of element names that you append() at the beginning of the function
>> and pop() at the end, i.e. a stack. That list will then always give you the
>> current path from the root node.
> 
> Thanks for the reply. How can I remove getiterator()? Then I won't be
> traversing the nodes of the tree. I can't iterate over tree. I am also
> unclear on where to do the pop(). I tried putting it just after the
> recursive call to parseChild() and I tried putting as the very last
> statement in parseChild() - neither one gave the desired result. Can
> you show me in code what you mean?

untested:

  nodes = []

  def process_subtree(c, path):
  name = c.get('Name') if c.tag == 'Node' else None
  if name:
  path.append(name)
  nodes.append('/'.join(path))

  for c1 in c:
  process_subtree(c1, path)

  if name:
  path.pop()

  process_subtree(tree.getroot(), [])


Stefan


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parsing nested unbounded XML fields with ElementTree

2013-11-26 Thread alister

On 26/11/13 11:59, Larry Martell wrote:

On Tue, Nov 26, 2013 at 5:41 AM, Alister  wrote:

On Mon, 25 Nov 2013 18:25:55 -0500, Larry Martell wrote:


On Mon, Nov 25, 2013 at 6:19 PM, Chris Angelico 
wrote:


On Tue, Nov 26, 2013 at 9:45 AM, Larry Martell

wrote:

On Monday, November 25, 2013 5:30:44 PM UTC-5, Chris Angelico wrote:


First off, please clarify: Are there five corresponding  tags
later on? If not, it's not XML, and nesting will have to be defined
some other way.

Yes, there are corresponding  tags. I just didn't show them.

Good good, I just saw the "unbounded" in your subject line and got
worried :) I'm pretty sure there's a way to parse that will preserve
the current nesting information, but others can describe that better
than I can.



The term 'unbounded' is used in the XML xsd file like this:





Secondly, please get off Google Groups. Your initial post is
malformed, and unless you specifically fight the software, your
replies will be even more malformed, to the point of being quite
annoying. There are many other ways to read a newsgroup, or you can
subscribe to the mailing list python-list@python.org, which carries
the same content.

Not sure what you mean by malformed. I don't really care for Google

Groups,

but I've been using it to post to this any other groups for years
(since

rn

and deja news went away) and no one ever said my posts were
malformed. In any case, I did not know the group was available as a
ML. I've subbed to that and will post that way.

The mailing list works well for me too. Google Groups is deceptively
easy for a lot of people, but if you look through the list's archives,
you'll see that the posts it makes are unwrapped (and thus string out
to the right an arbitrary length), and all quoted text is
double-spaced, among other problems. Its users are generally unaware of
this, and like you are not maliciously inflicting that on us all, but
that doesn't make it any less painful to read :) Thanks for switching.



I had noticed the double spacing and I always fixed that when I replied.
On Mon, Nov 25, 2013 at 6:19 PM, Chris Angelico ros...@gmail.com> wrote:

left-style:solid;padding-left:1ex">
class="im">On Tue, Nov 26, 2013 at 9:45 AM, Larry Martell larry.mart...@gmail.com>
wrote:

> On Monday, November 25, 2013 5:30:44 PM UTC-5, Chris Angelico
wrote:
>
>> First off, please clarify: Are there five
corresponding  tags
>> later on? If not, it's not XML, and nesting will have to be
defined
>> some other way.
>
> Yes, there are corresponding  tags. I just didn't
show them.

Good good, I just saw the "unbounded" in your subject
line and got
worried :) I'm pretty sure there's a way to parse that will
preserve
the current nesting information, but others can describe that better
than I can.The term
'unbounded' is used in the XML xsd file like
this:
left-style:solid;padding-left:1ex">


>> Secondly, please get off Google Groups. Your initial post
is
>> malformed, and unless you specifically fight the software,
your
>> replies will be even more malformed, to the point of being
quite
>> annoying. There are many other ways to read a newsgroup, or you
can
>> subscribe to the mailing list mailto:python-list@python.org";>python-list@python.org, which
carries
>> the same content.
>
> Not sure what you mean by malformed. I don't really care for
Google Groups,
> but I've been using it to post to this any other groups for
years (since rn
> and deja news went away) and no one ever said my posts were
malformed. In
> any case, I did not know the group was available as a ML. I've
subbed to
> that and will post that way.

The mailing list works well for me too. Google Groups is
deceptively
easy for a lot of people, but if you look through the list's
archives,
you'll see that the posts it makes are unwrapped (and thus string
out
to the right an arbitrary length), and all quoted text is
double-spaced, among other problems. Its users are generally unaware
of this, and like you are not maliciously inflicting that on us all,
but that doesn't make it any less painful to read :) Thanks for
switching.I had
noticed the double spacing and I always fixed that when I replied.
  


if you could now change your male client


What about my female client?


to send in plane text

How about plain text?


only we
would not get this duplicated HTML copy of the post which is just as
annoying as the double spacing form GG (probably more so).

Sorry, didn't realize it was sending in HMTL. I had it set to plain
text, but when the awful gmail update came out it seems to have
reverted to HTML. Hopefully this is better.

sorry, Typing too quickly without paying attention.

--
https://mail.python.org/mailman/listinfo/python-list


Re: parsing nested unbounded XML fields with ElementTree

2013-11-26 Thread Larry Martell
On Tue, Nov 26, 2013 at 2:38 AM, Stefan Behnel  wrote:
> larry.martell...@gmail.com, 25.11.2013 23:22:
>> I have an XML file that has an element called "Node". These can be nested to 
>> any depth and the depth of the nesting is not known to me. I need to parse 
>> the file and preserve the nesting. For exmaple, if the XML file had:
>>
>> 
>>
>>   
>> 
>>   
>>
>> When I'm parsing Node "E" I need to know I'm in A/B/C/D/E. Problem is I 
>> don't know how deep this can be. This is the code I have so far:
>>
>> nodes = []
>>
>> def parseChild(c):
>> if c.tag == 'Node':
>> if 'Name' in c.attrib:
>> nodes.append(c.attrib['Name'])
>> for c1 in c:
>> parseChild(c1)
>> else:
>> for node in nodes:
>> print node,
>> print c.tag
>>
>> for parent in tree.getiterator():
>> for child in parent:
>> for x in child:
>> parseChild(x)
>
> This seems hugely redundant. tree.getiterator() already returns a recursive
> iterable, and then, for each nodes in your document, you are running
> recursively over its entire subtree. Meaning that you'll visit each node as
> many times as its depth in the tree.
>
>
>> My problem is that I don't know when I'm done with a node and I should
>> remove a level of nesting. I would think this is a fairly common
>> situation, but I could not find any examples of parsing a file like
>> this. Perhaps I'm going about it completely wrong.
>
> Your recursive traversal function tells you when you're done. If you drop
> the getiterator() bit, reaching the end of parseChild() means that you're
> done with the element and start backing up. So you can simply pass down a
> list of element names that you append() at the beginning of the function
> and pop() at the end, i.e. a stack. That list will then always give you the
> current path from the root node.

Thanks for the reply. How can I remove getiterator()? Then I won't be
traversing the nodes of the tree. I can't iterate over tree. I am also
unclear on where to do the pop(). I tried putting it just after the
recursive call to parseChild() and I tried putting as the very last
statement in parseChild() - neither one gave the desired result. Can
you show me in code what you mean?

Thanks!
-larry

>
> Alternatively, if you want to use lxml.etree instead of ElementTree, you
> can use it's iterwalk() function, which gives you the same thing but
> without recursion, as a plain iterator.
>
> http://lxml.de/parsing.html#iterparse-and-iterwalk
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parsing nested unbounded XML fields with ElementTree

2013-11-26 Thread Chris Angelico
On Tue, Nov 26, 2013 at 10:59 PM, Larry Martell  wrote:
> Sorry, didn't realize it was sending in HMTL. I had it set to plain
> text, but when the awful gmail update came out it seems to have
> reverted to HTML. Hopefully this is better.

Yeah, I have the same trouble... but yes, this post looks fine to me.
(Do consider trimming quoted text, though.) It's text with no HTML
component.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parsing nested unbounded XML fields with ElementTree

2013-11-26 Thread Larry Martell
On Tue, Nov 26, 2013 at 5:41 AM, Alister  wrote:
>
> On Mon, 25 Nov 2013 18:25:55 -0500, Larry Martell wrote:
>
> > On Mon, Nov 25, 2013 at 6:19 PM, Chris Angelico 
> > wrote:
> >
> >> On Tue, Nov 26, 2013 at 9:45 AM, Larry Martell
> >> 
> >> wrote:
> >> > On Monday, November 25, 2013 5:30:44 PM UTC-5, Chris Angelico wrote:
> >> >
> >> >> First off, please clarify: Are there five corresponding  tags
> >> >> later on? If not, it's not XML, and nesting will have to be defined
> >> >> some other way.
> >> >
> >> > Yes, there are corresponding  tags. I just didn't show them.
> >>
> >> Good good, I just saw the "unbounded" in your subject line and got
> >> worried :) I'm pretty sure there's a way to parse that will preserve
> >> the current nesting information, but others can describe that better
> >> than I can.
> >>
> >>
> > The term 'unbounded' is used in the XML xsd file like this:
> >
> > 
> >
> >
> >> >> Secondly, please get off Google Groups. Your initial post is
> >> >> malformed, and unless you specifically fight the software, your
> >> >> replies will be even more malformed, to the point of being quite
> >> >> annoying. There are many other ways to read a newsgroup, or you can
> >> >> subscribe to the mailing list python-list@python.org, which carries
> >> >> the same content.
> >> >
> >> > Not sure what you mean by malformed. I don't really care for Google
> >> Groups,
> >> > but I've been using it to post to this any other groups for years
> >> > (since
> >> rn
> >> > and deja news went away) and no one ever said my posts were
> >> > malformed. In any case, I did not know the group was available as a
> >> > ML. I've subbed to that and will post that way.
> >>
> >> The mailing list works well for me too. Google Groups is deceptively
> >> easy for a lot of people, but if you look through the list's archives,
> >> you'll see that the posts it makes are unwrapped (and thus string out
> >> to the right an arbitrary length), and all quoted text is
> >> double-spaced, among other problems. Its users are generally unaware of
> >> this, and like you are not maliciously inflicting that on us all, but
> >> that doesn't make it any less painful to read :) Thanks for switching.
> >>
> >>
> > I had noticed the double spacing and I always fixed that when I replied.
> > On Mon, Nov 25, 2013 at 6:19 PM, Chris Angelico  > dir="ltr"> > target="_blank">ros...@gmail.com> wrote: > class="gmail_extra">
> >  > class="im">On Tue, Nov 26, 2013 at 9:45 AM, Larry Martell < > href="mailto:larry.mart...@gmail.com";>larry.mart...@gmail.com>
> > wrote:
> >
> > > On Monday, November 25, 2013 5:30:44 PM UTC-5, Chris Angelico
> > wrote:
> > >
> > >> First off, please clarify: Are there five
> > corresponding  tags
> > >> later on? If not, it's not XML, and nesting will have to be
> > defined
> > >> some other way.
> > >
> > > Yes, there are corresponding  tags. I just didn't
> > show them.
> > 
> > Good good, I just saw the "unbounded" in your subject
> > line and got
> > worried :) I'm pretty sure there's a way to parse that will
> > preserve
> > the current nesting information, but others can describe that better
> > than I can.The term
> > 'unbounded' is used in the XML xsd file like
> > this: > maxOccurs="unbounded"> > class="gmail_quote" style="margin:0px 0px 0px
> > 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-
> left-style:solid;padding-left:1ex">
> >
> > 
> > >> Secondly, please get off Google Groups. Your initial post
> > is
> > >> malformed, and unless you specifically fight the software,
> > your
> > >> replies will be even more malformed, to the point of being
> > quite
> > >> annoying. There are many other ways to read a newsgroup, or you
> > can
> > >> subscribe to the mailing list  > href="mailto:python-list@python.org";>python-list@python.org, which
> > carries
> > >> the same content.
> > >
> > > Not sure what you mean by malformed. I don't really care for
> > Google Groups,
> > > but I've been using it to post to this any other groups for
> > years (since rn
> > > and deja news went away) and no one ever said my posts were
> > malformed. In
> > > any case, I did not know the group was available as a ML. I've
> > subbed to
> > > that and will post that way.
> > 
> > The mailing list works well for me too. Google Groups is
> > deceptively
> > easy for a lot of people, but if you look through the list's
> > archives,
> > you'll see that the posts it makes are unwrapped (and thus string
> > out
> > to the right an arbitrary length), and all quoted text is
> > double-spaced, among other problems. Its users are generally unaware
> > of this, and like you are not maliciously inflicting that on us all,
> > but that doesn't make it any less painful to read :) Thanks for
> > switching. > color="#88">I had
> > noticed the double spacing and I always fixed that when I replied.
> >  
>
>
> if you could now change your male client


What about my

Re: parsing nested unbounded XML fields with ElementTree

2013-11-26 Thread Alister
On Mon, 25 Nov 2013 18:25:55 -0500, Larry Martell wrote:

> On Mon, Nov 25, 2013 at 6:19 PM, Chris Angelico 
> wrote:
> 
>> On Tue, Nov 26, 2013 at 9:45 AM, Larry Martell
>> 
>> wrote:
>> > On Monday, November 25, 2013 5:30:44 PM UTC-5, Chris Angelico wrote:
>> >
>> >> First off, please clarify: Are there five corresponding  tags
>> >> later on? If not, it's not XML, and nesting will have to be defined
>> >> some other way.
>> >
>> > Yes, there are corresponding  tags. I just didn't show them.
>>
>> Good good, I just saw the "unbounded" in your subject line and got
>> worried :) I'm pretty sure there's a way to parse that will preserve
>> the current nesting information, but others can describe that better
>> than I can.
>>
>>
> The term 'unbounded' is used in the XML xsd file like this:
> 
> 
> 
> 
>> >> Secondly, please get off Google Groups. Your initial post is
>> >> malformed, and unless you specifically fight the software, your
>> >> replies will be even more malformed, to the point of being quite
>> >> annoying. There are many other ways to read a newsgroup, or you can
>> >> subscribe to the mailing list python-list@python.org, which carries
>> >> the same content.
>> >
>> > Not sure what you mean by malformed. I don't really care for Google
>> Groups,
>> > but I've been using it to post to this any other groups for years
>> > (since
>> rn
>> > and deja news went away) and no one ever said my posts were
>> > malformed. In any case, I did not know the group was available as a
>> > ML. I've subbed to that and will post that way.
>>
>> The mailing list works well for me too. Google Groups is deceptively
>> easy for a lot of people, but if you look through the list's archives,
>> you'll see that the posts it makes are unwrapped (and thus string out
>> to the right an arbitrary length), and all quoted text is
>> double-spaced, among other problems. Its users are generally unaware of
>> this, and like you are not maliciously inflicting that on us all, but
>> that doesn't make it any less painful to read :) Thanks for switching.
>>
>>
> I had noticed the double spacing and I always fixed that when I replied.
> On Mon, Nov 25, 2013 at 6:19 PM, Chris Angelico  dir="ltr"> target="_blank">ros...@gmail.com> wrote: class="gmail_extra">
>  class="im">On Tue, Nov 26, 2013 at 9:45 AM, Larry Martell < href="mailto:larry.mart...@gmail.com";>larry.mart...@gmail.com>
> wrote:
> 
> > On Monday, November 25, 2013 5:30:44 PM UTC-5, Chris Angelico
> wrote:
> >
> >> First off, please clarify: Are there five
> corresponding  tags
> >> later on? If not, it's not XML, and nesting will have to be
> defined
> >> some other way.
> >
> > Yes, there are corresponding  tags. I just didn't
> show them.
> 
> Good good, I just saw the "unbounded" in your subject
> line and got
> worried :) I'm pretty sure there's a way to parse that will
> preserve
> the current nesting information, but others can describe that better
> than I can.The term
> 'unbounded' is used in the XML xsd file like
> this: maxOccurs="unbounded"> class="gmail_quote" style="margin:0px 0px 0px
> 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-
left-style:solid;padding-left:1ex">
> 
> 
> >> Secondly, please get off Google Groups. Your initial post
> is
> >> malformed, and unless you specifically fight the software,
> your
> >> replies will be even more malformed, to the point of being
> quite
> >> annoying. There are many other ways to read a newsgroup, or you
> can
> >> subscribe to the mailing list  href="mailto:python-list@python.org";>python-list@python.org, which
> carries
> >> the same content.
> >
> > Not sure what you mean by malformed. I don't really care for
> Google Groups,
> > but I've been using it to post to this any other groups for
> years (since rn
> > and deja news went away) and no one ever said my posts were
> malformed. In
> > any case, I did not know the group was available as a ML. I've
> subbed to
> > that and will post that way.
> 
> The mailing list works well for me too. Google Groups is
> deceptively
> easy for a lot of people, but if you look through the list's
> archives,
> you'll see that the posts it makes are unwrapped (and thus string
> out
> to the right an arbitrary length), and all quoted text is
> double-spaced, among other problems. Its users are generally unaware
> of this, and like you are not maliciously inflicting that on us all,
> but that doesn't make it any less painful to read :) Thanks for
> switching. color="#88">I had
> noticed the double spacing and I always fixed that when I replied.
>  


if you could now change your male client to send in plane text only we 
would not get this duplicated HTML copy of the post which is just as 
annoying as the double spacing form GG (probably more so).


-- 
 Bite me.
* TheOne gets some salt, then proceeds to nibble on KnaraKat a little
 bit
-- 
https://mail.python.org/mailman/listinfo/pytho

Re: parsing nested unbounded XML fields with ElementTree

2013-11-25 Thread Stefan Behnel
larry.martell...@gmail.com, 25.11.2013 23:22:
> I have an XML file that has an element called "Node". These can be nested to 
> any depth and the depth of the nesting is not known to me. I need to parse 
> the file and preserve the nesting. For exmaple, if the XML file had:
> 
> 
>
>   
> 
>   
> 
> When I'm parsing Node "E" I need to know I'm in A/B/C/D/E. Problem is I don't 
> know how deep this can be. This is the code I have so far:
> 
> nodes = []
> 
> def parseChild(c):
> if c.tag == 'Node':
> if 'Name' in c.attrib: 
> nodes.append(c.attrib['Name'])
> for c1 in c:
> parseChild(c1)
> else:
> for node in nodes:
> print node,
> print c.tag
> 
> for parent in tree.getiterator():
> for child in parent:
> for x in child:
> parseChild(x)

This seems hugely redundant. tree.getiterator() already returns a recursive
iterable, and then, for each nodes in your document, you are running
recursively over its entire subtree. Meaning that you'll visit each node as
many times as its depth in the tree.


> My problem is that I don't know when I'm done with a node and I should
> remove a level of nesting. I would think this is a fairly common
> situation, but I could not find any examples of parsing a file like
> this. Perhaps I'm going about it completely wrong.

Your recursive traversal function tells you when you're done. If you drop
the getiterator() bit, reaching the end of parseChild() means that you're
done with the element and start backing up. So you can simply pass down a
list of element names that you append() at the beginning of the function
and pop() at the end, i.e. a stack. That list will then always give you the
current path from the root node.

Alternatively, if you want to use lxml.etree instead of ElementTree, you
can use it's iterwalk() function, which gives you the same thing but
without recursion, as a plain iterator.

http://lxml.de/parsing.html#iterparse-and-iterwalk

Stefan


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parsing nested unbounded XML fields with ElementTree

2013-11-25 Thread Larry Martell
On Mon, Nov 25, 2013 at 6:19 PM, Chris Angelico  wrote:

> On Tue, Nov 26, 2013 at 9:45 AM, Larry Martell 
> wrote:
> > On Monday, November 25, 2013 5:30:44 PM UTC-5, Chris Angelico wrote:
> >
> >> First off, please clarify: Are there five corresponding  tags
> >> later on? If not, it's not XML, and nesting will have to be defined
> >> some other way.
> >
> > Yes, there are corresponding  tags. I just didn't show them.
>
> Good good, I just saw the "unbounded" in your subject line and got
> worried :) I'm pretty sure there's a way to parse that will preserve
> the current nesting information, but others can describe that better
> than I can.
>

The term 'unbounded' is used in the XML xsd file like this:




> >> Secondly, please get off Google Groups. Your initial post is
> >> malformed, and unless you specifically fight the software, your
> >> replies will be even more malformed, to the point of being quite
> >> annoying. There are many other ways to read a newsgroup, or you can
> >> subscribe to the mailing list python-list@python.org, which carries
> >> the same content.
> >
> > Not sure what you mean by malformed. I don't really care for Google
> Groups,
> > but I've been using it to post to this any other groups for years (since
> rn
> > and deja news went away) and no one ever said my posts were malformed. In
> > any case, I did not know the group was available as a ML. I've subbed to
> > that and will post that way.
>
> The mailing list works well for me too. Google Groups is deceptively
> easy for a lot of people, but if you look through the list's archives,
> you'll see that the posts it makes are unwrapped (and thus string out
> to the right an arbitrary length), and all quoted text is
> double-spaced, among other problems. Its users are generally unaware
> of this, and like you are not maliciously inflicting that on us all,
> but that doesn't make it any less painful to read :) Thanks for
> switching.
>
>
I had noticed the double spacing and I always fixed that when I replied.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parsing nested unbounded XML fields with ElementTree

2013-11-25 Thread Chris Angelico
On Tue, Nov 26, 2013 at 9:45 AM, Larry Martell  wrote:
> On Monday, November 25, 2013 5:30:44 PM UTC-5, Chris Angelico wrote:
>
>> First off, please clarify: Are there five corresponding  tags
>> later on? If not, it's not XML, and nesting will have to be defined
>> some other way.
>
> Yes, there are corresponding  tags. I just didn't show them.

Good good, I just saw the "unbounded" in your subject line and got
worried :) I'm pretty sure there's a way to parse that will preserve
the current nesting information, but others can describe that better
than I can.

>> Secondly, please get off Google Groups. Your initial post is
>> malformed, and unless you specifically fight the software, your
>> replies will be even more malformed, to the point of being quite
>> annoying. There are many other ways to read a newsgroup, or you can
>> subscribe to the mailing list python-list@python.org, which carries
>> the same content.
>
> Not sure what you mean by malformed. I don't really care for Google Groups,
> but I've been using it to post to this any other groups for years (since rn
> and deja news went away) and no one ever said my posts were malformed. In
> any case, I did not know the group was available as a ML. I've subbed to
> that and will post that way.

The mailing list works well for me too. Google Groups is deceptively
easy for a lot of people, but if you look through the list's archives,
you'll see that the posts it makes are unwrapped (and thus string out
to the right an arbitrary length), and all quoted text is
double-spaced, among other problems. Its users are generally unaware
of this, and like you are not maliciously inflicting that on us all,
but that doesn't make it any less painful to read :) Thanks for
switching.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parsing nested unbounded XML fields with ElementTree

2013-11-25 Thread Larry Martell
On Monday, November 25, 2013 5:30:44 PM UTC-5, Chris Angelico wrote:

> On Tue, Nov 26, 2013 at 9:22 AM, larry.mart...@gmail.com
>
>  wrote:
>
> > I have an XML file that has an element called "Node". These can be nested 
> > to any depth and the depth of the nesting is not known to me. I need to 
> > parse the file and preserve the nesting. For exmaple, if the XML file had:
>
> >
>
> > 
> >
> >   
> > 
> >   
>
>
>
> First off, please clarify: Are there five corresponding  tags
> later on? If not, it's not XML, and nesting will have to be defined
> some other way.

Yes, there are corresponding  tags. I just didn't show them.

> Secondly, please get off Google Groups. Your initial post is
> malformed, and unless you specifically fight the software, your
> replies will be even more malformed, to the point of being quite
> annoying. There are many other ways to read a newsgroup, or you can
> subscribe to the mailing list python-list@python.org, which carries
> the same content.

Not sure what you mean by malformed. I don't really care for Google
Groups, but I've been using it to post to this any other groups for
years (since rn and deja news went away) and no one ever said my posts
were malformed. In any case, I did not know the group was available as
a ML. I've subbed to that and will post that way.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parsing nested unbounded XML fields with ElementTree

2013-11-25 Thread Chris Angelico
On Tue, Nov 26, 2013 at 9:22 AM, larry.mart...@gmail.com
 wrote:
> I have an XML file that has an element called "Node". These can be nested to 
> any depth and the depth of the nesting is not known to me. I need to parse 
> the file and preserve the nesting. For exmaple, if the XML file had:
>
> 
>
>   
> 
>   

First off, please clarify: Are there five corresponding  tags
later on? If not, it's not XML, and nesting will have to be defined
some other way.

Secondly, please get off Google Groups. Your initial post is
malformed, and unless you specifically fight the software, your
replies will be even more malformed, to the point of being quite
annoying. There are many other ways to read a newsgroup, or you can
subscribe to the mailing list python-list@python.org, which carries
the same content.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


parsing nested unbounded XML fields with ElementTree

2013-11-25 Thread larry.mart...@gmail.com
I have an XML file that has an element called "Node". These can be nested to 
any depth and the depth of the nesting is not known to me. I need to parse the 
file and preserve the nesting. For exmaple, if the XML file had:


   
  

  

When I'm parsing Node "E" I need to know I'm in A/B/C/D/E. Problem is I don't 
know how deep this can be. This is the code I have so far:

nodes = []

def parseChild(c):
if c.tag == 'Node':
if 'Name' in c.attrib: 
nodes.append(c.attrib['Name'])
for c1 in c:
parseChild(c1)
else:
for node in nodes:
print node,
print c.tag

for parent in tree.getiterator():
for child in parent:
for x in child:
parseChild(x)

My problem is that I don't know when I'm done with a node and I should remove a 
level of nesting. I would think this is a fairly common situation, but I could 
not find any examples of parsing a file like this. Perhaps I'm going about it 
completely wrong.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ElementTree: can't figure out a mismached-tag error

2013-07-11 Thread fronagzen
On Thursday, July 11, 2013 8:25:13 PM UTC+8, F.R. wrote:
> On 07/11/2013 10:59 AM, F.R. wrote:
> 
> > Hi all,
> 
> >
> 
> > I haven't been able to get up to speed with XML. I do examples from 
> 
> > the tutorials and experiment with variations. Time and time again I 
> 
> > fail with errors messages I can't make sense of. Here's the latest 
> 
> > one. The url is "http://finance.yahoo.com/q?s=XIDEQ&ql=0";. Ubuntu 
> 
> > 12.04 LTS, Python 2.7.3 (default, Aug  1 2012, 05:16:07) [GCC 4.6.3]
> 
> >
> 
> > >>> import xml.etree.ElementTree as ET
> 
> > >>> tree = ET.parse('q?s=XIDEQ')  # output of wget 
> 
> > http://finance.yahoo.com/q?s=XIDEQ&ql=0
> 
> > Traceback (most recent call last):
> 
> >   File "", line 1, in 
> 
> > tree = ET.parse('q?s=XIDEQ')
> 
> >   File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1183, in parse
> 
> > tree.parse(source, parser)
> 
> >   File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 656, in parse
> 
> > parser.feed(data)
> 
> >   File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1643, in feed
> 
> > self._raiseerror(v)
> 
> >   File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1507, in 
> 
> > _raiseerror
> 
> > raise err
> 
> > ParseError: mismatched tag: line 9, column 2
> 
> >
> 
> > Below first nine lines. The line numbers and the following space are 
> 
> > hand-edited in. Three dots stand for sections cut out to fit long 
> 
> > lines. Line 6 is a bunch of "meta" statements, all of which I show on 
> 
> > a separate line each in order to preserve the angled brackets. On all 
> 
> > lines the angled brackets have been preserved. The mismatched 
> 
> > character is the slash of the closing tag . What could be wrong 
> 
> > with it? And if it is, what about fault tolerance?
> 
> >
> 
> > 1 
> 
> > 2 
> 
> > 3 
> 
> > 4 XIDEQ: Summary for EXIDE TECH NEW- Yahoo! Finance
> 
> > 5 
> 
> >   
> 
> >   
> 
> >   
> 
> >   
> 
> >   
> 
> >
> > content="http://l.yimg.com/a/p/fi/31/09/00.jpg";>
> 
> >   http://finance.yahoo.com/q?s=XIDEQ";>
> 
> >
> > href="http://finance.yahoo.com/q?s=XIDEQ";>
> 
> > 8 http://l.yimg.com/zz/ . . . 
> 
> > type="text/css">
> 
> > 9 
> 
> >^
> 
> > Mismatch!
> 
> >
> 
> > Thanks for suggestions
> 
> >
> 
> > Frederic
> 
> >
> 
> Thank you all!
> 
> 
> 
> I was a little apprehensive it could be a silly mistake. And so it was. 
> 
> I have BeautifulSoup somewhere. Having had no urgent need for it I 
> 
> remember shirking the learning curve.
> 
> 
> 
> lxml seems to be a package with these components (from help (lxml)):
> 
> 
> 
> PACKAGE CONTENTS
> 
>  ElementInclude
> 
>  _elementpath
> 
>  builder
> 
>  cssselect
> 
>  doctestcompare
> 
>  etree
> 
>  html (package)
> 
>  isoschematron (package)
> 
>  objectify
> 
>  pyclasslookup
> 
>  sax
> 
>  usedoctest
> 
> 
> 
> I would start with "from lxml import html" and see what comes out.
> 
> 
> 
> Break time now. Thanks again!
> 
> 
> 
> Frederic

from lxml.html import parse
from lxml.etree import ElementTree
root = parse(target_url).getroot()

This'll get you the root node of the element tree parsed from the URL. The lxml 
html parser, conveniently enough, can combine in the actual web page access. If 
you want to control things like socket timeout, though, you'll have to use 
urllib to request the URL and then feed that to the parser.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree: can't figure out a mismached-tag error

2013-07-11 Thread F.R.

On 07/11/2013 10:59 AM, F.R. wrote:

Hi all,

I haven't been able to get up to speed with XML. I do examples from 
the tutorials and experiment with variations. Time and time again I 
fail with errors messages I can't make sense of. Here's the latest 
one. The url is "http://finance.yahoo.com/q?s=XIDEQ&ql=0";. Ubuntu 
12.04 LTS, Python 2.7.3 (default, Aug  1 2012, 05:16:07) [GCC 4.6.3]


>>> import xml.etree.ElementTree as ET
>>> tree = ET.parse('q?s=XIDEQ')  # output of wget 
http://finance.yahoo.com/q?s=XIDEQ&ql=0

Traceback (most recent call last):
  File "", line 1, in 
tree = ET.parse('q?s=XIDEQ')
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1183, in parse
tree.parse(source, parser)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 656, in parse
parser.feed(data)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1643, in feed
self._raiseerror(v)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1507, in 
_raiseerror

raise err
ParseError: mismatched tag: line 9, column 2

Below first nine lines. The line numbers and the following space are 
hand-edited in. Three dots stand for sections cut out to fit long 
lines. Line 6 is a bunch of "meta" statements, all of which I show on 
a separate line each in order to preserve the angled brackets. On all 
lines the angled brackets have been preserved. The mismatched 
character is the slash of the closing tag . What could be wrong 
with it? And if it is, what about fault tolerance?


1 
2 
3 

4 XIDEQ: Summary for EXIDE TECH NEW- Yahoo! Finance
5 

  
  
  
  
  
  content="http://l.yimg.com/a/p/fi/31/09/00.jpg";>

  http://finance.yahoo.com/q?s=XIDEQ";>
  href="http://finance.yahoo.com/q?s=XIDEQ";>
8 http://l.yimg.com/zz/ . . . 
type="text/css">

9 
   ^
Mismatch!

Thanks for suggestions

Frederic


Thank you all!

I was a little apprehensive it could be a silly mistake. And so it was. 
I have BeautifulSoup somewhere. Having had no urgent need for it I 
remember shirking the learning curve.


lxml seems to be a package with these components (from help (lxml)):

PACKAGE CONTENTS
ElementInclude
_elementpath
builder
cssselect
doctestcompare
etree
html (package)
isoschematron (package)
objectify
pyclasslookup
sax
usedoctest

I would start with "from lxml import html" and see what comes out.

Break time now. Thanks again!

Frederic

--
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree: can't figure out a mismached-tag error

2013-07-11 Thread Fábio Santos
On 11 Jul 2013 10:24,  wrote:
>
> Actually, I don't think etree has a HTML parser. And I would
counter-recommend lxml if speed is an issue: BeautifulSoup takes a long
time to parse a large document.
>
> On Thursday, July 11, 2013 5:08:04 PM UTC+8, Fábio Santos wrote:
> >
> > Use an HTML parser. I strongly recommend  BeautifulSoup but I think
etree has an HTML parser too. I am not sure..

I meant lxml. My apologies.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree: can't figure out a mismached-tag error

2013-07-11 Thread fronagzen
Actually, I don't think etree has a HTML parser. And I would counter-recommend 
lxml if speed is an issue: BeautifulSoup takes a long time to parse a large 
document.

On Thursday, July 11, 2013 5:08:04 PM UTC+8, Fábio Santos wrote:
> On 11 Jul 2013 10:04, "F.R."  wrote:
> 
> >
> 
> > Hi all,
> 
> >
> 
> > I haven't been able to get up to speed with XML. I do examples from the 
> > tutorials and experiment with variations. Time and time again I fail with 
> > errors messages I can't make sense of. Here's the latest one. The url is 
> > "http://finance.yahoo.com/q?s=XIDEQ&ql=0";. Ubuntu 12.04 LTS, Python 2.7.3 
> > (default, Aug  1 2012, 05:16:07) [GCC 4.6.3]
> 
> 
> >
> 
> > >>> import xml.etree.ElementTree as ET
> 
> > >>> tree = ET.parse('q?s=XIDEQ')  # output of wget 
> > >>> http://finance.yahoo.com/q?s=XIDEQ&ql=0
> 
> > Traceback (most recent call last):
> 
> >   File "", line 1, in 
> 
> >     tree = ET.parse('q?s=XIDEQ')
> 
> >   File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1183, in parse
> 
> >     tree.parse(source, parser)
> 
> >   File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 656, in parse
> 
> >     parser.feed(data)
> 
> >   File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1643, in feed
> 
> >     self._raiseerror(v)
> 
> >   File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1507, in 
> > _raiseerror
> 
> >     raise err
> 
> > ParseError: mismatched tag: line 9, column 2
> 
> >
> 
> > Below first nine lines. The line numbers and the following space are 
> > hand-edited in. Three dots stand for sections cut out to fit long lines. 
> > Line 6 is a bunch of "meta" statements, all of which I show on a separate 
> > line each in order to preserve the angled brackets. On all lines the angled 
> > brackets have been preserved. The mismatched character is the slash of the 
> > closing tag . What could be wrong with it? And if it is, what about 
> > fault tolerance?
> 
> 
> >
> 
> > 1 
> 
> > 2 
> 
> > 3 
> 
> > 4 XIDEQ: Summary for EXIDE TECH NEW- Yahoo! Finance
> 
> > 5 
> 
> >   
> 
> >   
> 
> >   
> 
> >   
> 
> >   
> 
> >   http://l.yimg.com/a/p/fi/31/09/00.jpg";>
> 
> >   http://finance.yahoo.com/q?s=XIDEQ";>
> 
> >    > href="http://finance.yahoo.com/q?s=XIDEQ";>
> 
> > 8 http://l.yimg.com/zz/ . . . type="text/css">
> 
> > 9 
> 
> >    ^
> 
> >     Mismatch!
> 
> >
> 
> > Thanks for suggestions
> 
> >
> 
> > Frederic
> 
> That is not XML. It is HTML. You get a mismatched tag because the  tag 
> doesn't need closing in HTML, but in XML every single tag needs closing.
> 
> Use an HTML parser. I strongly recommend  BeautifulSoup but I think etree has 
> an HTML parser too. I am not sure..

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree: can't figure out a mismached-tag error

2013-07-11 Thread Fábio Santos
On 11 Jul 2013 10:04, "F.R."  wrote:
>
> Hi all,
>
> I haven't been able to get up to speed with XML. I do examples from the
tutorials and experiment with variations. Time and time again I fail with
errors messages I can't make sense of. Here's the latest one. The url is "
http://finance.yahoo.com/q?s=XIDEQ&ql=0";. Ubuntu 12.04 LTS, Python 2.7.3
(default, Aug  1 2012, 05:16:07) [GCC 4.6.3]
>
> >>> import xml.etree.ElementTree as ET
> >>> tree = ET.parse('q?s=XIDEQ')  # output of wget
http://finance.yahoo.com/q?s=XIDEQ&ql=0
> Traceback (most recent call last):
>   File "", line 1, in 
> tree = ET.parse('q?s=XIDEQ')
>   File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1183, in parse
> tree.parse(source, parser)
>   File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 656, in parse
> parser.feed(data)
>   File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1643, in feed
> self._raiseerror(v)
>   File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1507, in
_raiseerror
> raise err
> ParseError: mismatched tag: line 9, column 2
>
> Below first nine lines. The line numbers and the following space are
hand-edited in. Three dots stand for sections cut out to fit long lines.
Line 6 is a bunch of "meta" statements, all of which I show on a separate
line each in order to preserve the angled brackets. On all lines the angled
brackets have been preserved. The mismatched character is the slash of the
closing tag . What could be wrong with it? And if it is, what about
fault tolerance?
>
> 1 
> 2 
> 3 
> 4 XIDEQ: Summary for EXIDE TECH NEW- Yahoo! Finance
> 5 
>   
>   
>   
>   
>   
>   http://l.yimg.com/a/p/fi/31/09/00.jpg
">
>   http://finance.yahoo.com/q?s=XIDEQ";>
>   http://finance.yahoo.com/q?s=XIDEQ";>
> 8 http://l.yimg.com/zz/ . . .
type="text/css">
> 9 
>^
> Mismatch!
>
> Thanks for suggestions
>
> Frederic

That is not XML. It is HTML. You get a mismatched tag because the 
tag doesn't need closing in HTML, but in XML every single tag needs closing.

Use an HTML parser. I strongly recommend  BeautifulSoup but I think etree
has an HTML parser too. I am not sure..
-- 
http://mail.python.org/mailman/listinfo/python-list


ElementTree: can't figure out a mismached-tag error

2013-07-11 Thread F.R.

Hi all,

I haven't been able to get up to speed with XML. I do examples from the 
tutorials and experiment with variations. Time and time again I fail 
with errors messages I can't make sense of. Here's the latest one. The 
url is "http://finance.yahoo.com/q?s=XIDEQ&ql=0";. Ubuntu 12.04 LTS, 
Python 2.7.3 (default, Aug  1 2012, 05:16:07) [GCC 4.6.3]


>>> import xml.etree.ElementTree as ET
>>> tree = ET.parse('q?s=XIDEQ')  # output of wget 
http://finance.yahoo.com/q?s=XIDEQ&ql=0

Traceback (most recent call last):
  File "", line 1, in 
tree = ET.parse('q?s=XIDEQ')
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1183, in parse
tree.parse(source, parser)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 656, in parse
parser.feed(data)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1643, in feed
self._raiseerror(v)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1507, in 
_raiseerror

raise err
ParseError: mismatched tag: line 9, column 2

Below first nine lines. The line numbers and the following space are 
hand-edited in. Three dots stand for sections cut out to fit long lines. 
Line 6 is a bunch of "meta" statements, all of which I show on a 
separate line each in order to preserve the angled brackets. On all 
lines the angled brackets have been preserved. The mismatched character 
is the slash of the closing tag . What could be wrong with it? 
And if it is, what about fault tolerance?


1 
2 
3 
4 XIDEQ: Summary for EXIDE TECH NEW- Yahoo! Finance
5 

  
  
  
  
  
  content="http://l.yimg.com/a/p/fi/31/09/00.jpg";>

  http://finance.yahoo.com/q?s=XIDEQ";>
  href="http://finance.yahoo.com/q?s=XIDEQ";>

8 http://l.yimg.com/zz/ . . . type="text/css">
9 
   ^
Mismatch!

Thanks for suggestions

Frederic

--
http://mail.python.org/mailman/listinfo/python-list


XML to PDF book with ElementTree and xtopdf

2013-06-15 Thread vasudevram

Hi list,

This may be of interest - a program to create simple PDF books from XML text 
content:

Create PDF books with XMLtoPDFBook:

http://jugad2.blogspot.in/2013/06/create-pdf-books-with-xmltopdfbook.html

XMLtoPDFBook.py requires ElementTree (which is in the standard Python library), 
xtopdf, ReportLab and Python.

Relevant download links for xtopdf and ReportLab are in the post linked above.

Here is a guide to installing xtopdf:

http://jugad2.blogspot.in/2012/07/guide-to-installing-and-using-xtopdf.html

- Vasudev Ram
Python, Linux and open source consulting and training
Site: http://dancingbison.com
Blog: http://jugad2.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Create xml with elementtree ET and xml escaping

2012-12-15 Thread Stefan Behnel
nenad.ci...@gmail.com, 12.12.2012 03:19:
> Il giorno martedì 11 dicembre 2012 20:59:54 UTC+1, MRAB ha scritto:
>>
>>> Since I have also the need to sign the XML I need the ability to create xml 
>>> but without xml escaping (unescaped data are signed).
>>
>> XML with the escaping isn't valid XML.
> 
> Of course I know it is not valid without escaping. But I need it only for 
> signing. I will recheck this if really the web service wants the data to be 
> signed as non escaped.

If it expects non-XML, you should tell the owners of the web service so
that they can fix it.

Stefan


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Create xml with elementtree ET and xml escaping

2012-12-11 Thread nenad . cikic
Il giorno martedì 11 dicembre 2012 20:59:54 UTC+1, MRAB ha scritto:
> > Hello, I have posted the same in XML group but it seems pretty dead there 
> > so I will repost here.
> 
> >
> 
> > I am new to xml processing in python.
> 
> > I am looking to create XML. Xml is not too difficult so I thought to create 
> > it manually using ElementTree.
> 
> > First I noted that ET.toString does escape <>& but not " and '
> 
> > Is that normal?
> 
> >
> 
> " needs to be encoded when it's in an attribute's value:
> 
> 
> 
>  


OK I understood.
 
> 
> 
> because it's also being used as a delimiter in that case, but elsewhere
> 
> it has no special meaning.
> 
> 
> 
> > Since I have also the need to sign the XML I need the ability to create xml 
> > but without xml escaping (unescaped data are signed).
> 
> 
> 
> XML with the escaping isn't valid XML.
> 

Of course I know it is not valid without escaping. But I need it only for 
signing. I will recheck this if really the web service wants the data to be 
signed as non escaped.
 
Thanks
Nenad
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Create xml with elementtree ET and xml escaping

2012-12-11 Thread MRAB

On 2012-12-11 17:47, nenad.ci...@gmail.com wrote:

Hello, I have posted the same in XML group but it seems pretty dead there so I 
will repost here.

I am new to xml processing in python.
I am looking to create XML. Xml is not too difficult so I thought to create it 
manually using ElementTree.
First I noted that ET.toString does escape <>& but not " and '
Is that normal?


" needs to be encoded when it's in an attribute's value:



because it's also being used as a delimiter in that case, but elsewhere
it has no special meaning.


Since I have also the need to sign the XML I need the ability to create xml but 
without xml escaping (unescaped data are signed).


XML with the escaping isn't valid XML.


If i do ET.toString(root,'utf8',text') i do not get the xml tags and if I do 
ET.toString(root,'utf8') I get escaped chars.
For example:
a=ET.Element('a')
b=ET.SubElement(a,'b')
b.text=u"šđ<>&"

ET.tostring(a,'utf8')
outputs to
"\n\xc5\xa1\xc4\x91<>&"

ET.tostring(a,'utf8',method='text')
outputs to
"\xc5\xa1\xc4\x91<>&"

and I need before singing
\xc5\xa1\xc4\x91<>&
and after signing
\xc5\xa1\xc4\x91<>&

Is there some way other than string replace?



--
http://mail.python.org/mailman/listinfo/python-list


Create xml with elementtree ET and xml escaping

2012-12-11 Thread nenad . cikic
Hello, I have posted the same in XML group but it seems pretty dead there so I 
will repost here.

I am new to xml processing in python.
I am looking to create XML. Xml is not too difficult so I thought to create it 
manually using ElementTree.
First I noted that ET.toString does escape <>& but not " and '
Is that normal?

Since I have also the need to sign the XML I need the ability to create xml but 
without xml escaping (unescaped data are signed).
If i do ET.toString(root,'utf8',text') i do not get the xml tags and if I do 
ET.toString(root,'utf8') I get escaped chars.
For example:
a=ET.Element('a')
b=ET.SubElement(a,'b')
b.text=u"šđ<>&"

ET.tostring(a,'utf8')
outputs to
"\n\xc5\xa1\xc4\x91<>&"

ET.tostring(a,'utf8',method='text')
outputs to
"\xc5\xa1\xc4\x91<>&"

and I need before singing
\xc5\xa1\xc4\x91<>&
and after signing
\xc5\xa1\xc4\x91<>&

Is there some way other than string replace?
Thanks
Nenad
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree Issue - Search and remove elements

2012-10-17 Thread Stefan Behnel
Alain Ketterlin, 17.10.2012 08:25:
> It looks like you can't get the parent of an Element with elementtree (I
> would love to be proven wrong on this).

No, that's by design. ElementTree allows you to reuse subtrees in a
document, for example, which wouldn't work if you enforced a single parent.
Also, keeping parent references out simplifies the tree structure
considerably, saves space and time and all that. ElementTree is really
great for what it does.

If you need to access the parent more often in a read-only tree, you can
quickly build up a back reference dict that maps each Element to its parent
by traversing the tree once.

Alternatively, use lxml.etree, in which Elements have a getparent() method
and in which single parents are enforced (also by design).

Stefan


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree Issue - Search and remove elements

2012-10-16 Thread Alain Ketterlin
Tharanga Abeyseela  writes:

> I need to remove the parent node, if a particular match found.

It looks like you can't get the parent of an Element with elementtree (I
would love to be proven wrong on this).

The solution is to find all nodes that have a Rating (grand-) child, and
then test explicitly for the value you're looking for.

> 
> http://schemas..xx/xx/2011/06/13/xx";>
> 
[...]
> 
> 
> M


> for child in 
> root.findall(".//{http://schemas.CCC.com/CCC/2011/06/13/CC}Rating";):
>x = child.find('Rating').text
> if child[1].text == 'NC':
> print "found"
>root.remove('TVEpisode') ?

Your code doesn't work because findall() already returns Rating
elements, and these have no Rating child (so your first call to find()
fails, i.e., returns None). And list indexes starts at 0, btw.

Also, Rating is not a child of TVEpisode, it is a child of
ParentalControl.

Here is my suggestion:

# Find nodes having a ParentalControl child
for child in root.findall(".//*[ParentalControl]"):
x = child.find("ParentalControl/Rating").text
if x == "NC":
...

Note that a complete XPath implementation would make that simpler: your
query basically is //*[ParentalControl/Rating=="NC"]

-- Alain.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree Issue - Search and remove elements

2012-10-16 Thread Stefan Behnel
Hi,

note that it's best to reply to responses you get, rather than starting a
new thread on the same topic. It helps in building up context and in
keeping details together at one point in the archive for users who run into
similar problems later.

Tharanga Abeyseela, 17.10.2012 07:47:
> I need to remove the parent node, if a particular match found.
> 
> ex:
> 
> 
> http://schemas..xx/xx/2011/06/13/xx";>
> 
> 0x5
> http://fxxl
> WWE
> WWE 
> WWE
> false
> 
> 
> BoxArt
> https://xx.xx/@006548-thumb.jpg
> 
> 
> 2012-10-16T00:00:19.814+11:00
> 
> x
> 
> 
> 
> M
> 
> 
> if i found NC, i need to remove the  from
> the XML. i have TVseries,Movies,and several items. (they also have
> Rating element). i need to remove all if i found the NC keyword.inside
> 
> 
> 
> im using following code.
> 
> when i do the following on python shell  i can see the result (NC,M,etc)
> 
> >>> x[1].text
> 'NC'
> 
> but when i do this inside the script, im getting the following error.
> 
> Traceback (most recent call last):
>   File "./test.py", line 10, in ?
> x = child.find('Rating').text
> AttributeError: 'NoneType' object has no attribute 'text'
> 
> 
> but how should i remove the parent node if i found the string "NC" i
> need to do this for all elements (TVEpisode,Movies,TVshow etc)
> how can i use python to remove the parent node if that string found.
> (not only TVEpisodes, but others as well)
> 
> 
> #!/usr/bin/env python
> 
> import elementtree.ElementTree as ET
> 
> tree = ET.parse('test.xml')
> root = tree.getroot()
> 
> 
> for child in 
> root.findall(".//{http://schemas.CCC.com/CCC/2011/06/13/CC}Rating";):
>x = child.find('Rating').text
> if child[1].text == 'NC':
> print "found"
>root.remove('TVEpisode') ?
> tree.write('output.xml')

The trick is to search for the parent node, then let your code find out if
you need to remove it or not by traversing its subtree.

Stefan


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting involved - Jython ElementTree performance

2012-01-07 Thread Stefan Behnel
Sophie Sperner, 07.01.2012 13:01:
> On Jan 7, 11:53 am, Stefan Behnel wrote:
>> A big issue that I have with Jython is that its ElementTree XML parser
>> support is so increadibly slow. It could seriously benefit from a better
>> integration between the Java XML support it uses and the Python ElementTree
>> module that it inherits from CPython. Considering that XML is a requirement
>> in many real world applications, especially in the Java world, you (Sophie)
>> might be interested in adding all three of Python, Java and XML to your
>> toolbox at the same time, while also learning how to benchmark and optimise
>> Java and Python code. I think that would pretty quickly get you up to speed
>> with real world software development.
> 
> Sounds interesting. Are you a developer or user of Jython?

More of a user. I'm actually developing lxml, a fast XML library for
CPython that reimplements ElementTree. When working with Java, I try to
keep using ElementTree in Jython because it gives me a simple to use XML
interface, but it's sometimes hard to defend its use because the parser
part is so slow - several times slower than lxml would run here.


> Can you tell me more about this particular issue? Or it is easier to
> check jython.org?

Here's a bit of a discussion:

http://sourceforge.net/mailarchive/message.php?msg_id=24946690

The current integration is more at a "make it work" than a "make it fast"
level. Specifically, the "expat" replacement module has several problems,
also at an algorithmic level, that make it slow.

I did some benchmarking, actually not on Jython, but at least comparing
CPython and PyPy:

http://blog.behnel.de/index.php?p=210

I posted the benchmarking code here:

http://mail.python.org/pipermail/python-dev/2011-December/115111.html

You can use the attached script to see for yourself. It should also run on
Jython, so you can use it to compare the performance of ElementTree in
CPython and Jython on your own machine.

That being said, yes, I'd suggest discussing this on a Jython related list.
I cross-posted it to the jython-devel mailing list, please discuss anything
related to Jython development over there.

Stefan

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml, minidom, ElementTree

2011-12-14 Thread Paul Rudin
Ethan Furman  writes:

> In the near future I will need to parse and rewrite parts of a xml files
> created by a third-party program (PrintShopMail, for the curious).
> It contains both binary and textual data.
>
> There has been some strong debate about the merits of minidom vs
> ElementTree.
>
> Recommendations?

I tend to start with BeautifulSoup, and think about other things if that
doesn't work out. It might just be me, but I find it easier to get stuff
done using BeautifulSoup than either minidom or ElementTree.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml, minidom, ElementTree

2011-12-13 Thread Stefan Behnel

Terry Reedy, 14.12.2011 06:01:

On 12/13/2011 6:21 PM, Ethan Furman wrote:

In the near future I will need to parse and rewrite parts of a xml files
created by a third-party program (PrintShopMail, for the curious).
It contains both binary and textual data.

There has been some strong debate about the merits of minidom vs
ElementTree.

Recommendations?


People's reaction to the DOM interface seem quite varied, with a majority,
perhaps, being negative. I personally would look at both enough to
understand the basic API model to see where *I* fit.


The API is one thing, yes, but there's also the fact that MiniDOM doesn't 
scale. If your XML files are of a notable size (a couple of MB), MiniDOM 
may simply not be able to handle them. I collected some numbers in a blog 
post. Note that this is using a recent CPython 3.3 build which has an 
optimised Unicode string implementation, thus yielding lower memory 
requirements on average than Py2.x.


http://blog.behnel.de/index.php?p=197

The memory consumption makes a difference of a factor of 5-10 compared to 
cElementTree, which makes it two orders of magnitude larger than the size 
of the serialised file. You may be able to stuff one such file into memory, 
but you'll quickly get into trouble when you try to do parallel processing 
or otherwise use more than one document at a time.


Stefan

--
http://mail.python.org/mailman/listinfo/python-list


Re: xml, minidom, ElementTree

2011-12-13 Thread Terry Reedy

On 12/13/2011 6:21 PM, Ethan Furman wrote:

In the near future I will need to parse and rewrite parts of a xml files
created by a third-party program (PrintShopMail, for the curious).
It contains both binary and textual data.

There has been some strong debate about the merits of minidom vs
ElementTree.

Recommendations?


People's reaction to the DOM interface seem quite varied, with a 
majority, perhaps, being negative. I personally would look at both 
enough to understand the basic API model to see where *I* fit.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


xml, minidom, ElementTree

2011-12-13 Thread Ethan Furman

In the near future I will need to parse and rewrite parts of a xml files
created by a third-party program (PrintShopMail, for the curious).
It contains both binary and textual data.

There has been some strong debate about the merits of minidom vs 
ElementTree.


Recommendations?

~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


Re: xml tree writing with ElementTree; prepends elements with ns0

2011-10-11 Thread Alain Ketterlin
Alain Ketterlin  writes:

> "Alex van der Spek"  writes:
>
>> When reading a tree and writing it back to a new file all the elements are 
>> prepended with the string ns0:
>
> That's a namespace prefix.
>
>>
>> Why is it prepended and how can I suppress this?
>
> See http://effbot.org/zone/element-namespaces.htm
>
> I'm not sure you can define the default namespace (i.e., avoid prefixes
> on element names). However, any conformant XML processor should have no
> problem with the output of ElementTree.

Sorry, it looks like you can with ET 1.3: see
http://effbot.org/zone/elementtree-13-intro.htm

> If you're actually producing HTML, then you should say so when calling
> tostring(), by giving the appropriate value to the method argument.
>
> -- Alain.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml tree writing with ElementTree; prepends elements with ns0

2011-10-11 Thread Alain Ketterlin
"Alex van der Spek"  writes:

> When reading a tree and writing it back to a new file all the elements are 
> prepended with the string ns0:

That's a namespace prefix.

>
> Why is it prepended and how can I suppress this?

See http://effbot.org/zone/element-namespaces.htm

I'm not sure you can define the default namespace (i.e., avoid prefixes
on element names). However, any conformant XML processor should have no
problem with the output of ElementTree.

If you're actually producing HTML, then you should say so when calling
tostring(), by giving the appropriate value to the method argument.

-- Alain.
-- 
http://mail.python.org/mailman/listinfo/python-list


xml tree writing with ElementTree; prepends elements with ns0

2011-10-11 Thread Alex van der Spek
When reading a tree and writing it back to a new file all the elements are 
prepended with the string ns0:

Why is it prepended and how can I suppress this?

Thanks,
Alex van der Spek 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree XML parsing problem

2011-04-27 Thread Ervin Hegedüs
hello,

On Thu, Apr 28, 2011 at 07:57:28AM +0200, Stefan Behnel wrote:
> >So, I started change the codepage mark of xml:
> >
> >  - same result
> >  - same result
> >  - same result
> 
> You probably changed this in an editor that supports XML and thus
> saves the file in the declared encoding.

no. I've saved the XML as UTF8, and didn't change the _file_
encoding - just modified the XML header, nothing else...

(I'm using Geany - it doesn't realize what user wrote in file,
just can save file as another encodign, when user choose one)


> Switching between the three
> by simply changing the first line (the XML declaration) and not
> adapting the encoding of the document itself would otherwise not
> yield the same result for the document given above.

yes, that's what I wrote exactly.


a. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree XML parsing problem

2011-04-27 Thread Stefan Behnel

Hegedüs Ervin, 27.04.2011 21:33:

hello,


I'm using ElementTree to parse an XML file, but it stops at the
second record (id = 002), which contains a non-standard ascii
character, ä. Here's the XML:










The complaint offered up by the parser is


I've checked this xml with your script, I think your locales
settings are not good.

$ ./parse.py

XML file: test.xml
001 High School
002 Universität Bremen
003 River College

(name of xml file is "test.xml")

So, I started change the codepage mark of xml:

  - same result
  - same result
  - same result


You probably changed this in an editor that supports XML and thus saves the 
file in the declared encoding. Switching between the three by simply 
changing the first line (the XML declaration) and not adapting the encoding 
of the document itself would otherwise not yield the same result for the 
document given above.


Stefan

--
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree XML parsing problem

2011-04-27 Thread Mike

On 4/27/2011 12:24 PM, Neil Cerutti wrote:

On 2011-04-27, Mike  wrote:

I'm using ElementTree to parse an XML file, but it stops at the
second record (id = 002), which contains a non-standard ascii
character, ?. Here's the XML:










The complaint offered up by the parser is

Unexpected error opening simple_fail.xml: not well-formed
(invalid token): line 5, column 40


It seems to be an invalid XML document, as another poster
indicated.


and if I change the line to eliminate the ?, everything is
wonderful. The parser is perfectly happy with this
modification:



I can't find anything in the ElementTree docs about allowing
additional text characters or coercing strange ascii to
Unicode.


If you're not the one generating that bogus file, then you can
specify the encoding yourself instead by declaring an XMLParser.

   import xml.etree.ElementTree as etree
   with open('file.xml') as xml_file:
 parser = etree.XMLParser(encoding='ISO-8859-1')
 root = etree.parse(xml_file, parser=parser).getroot()



Thanks, Neil. I'm not generating the file, just trying to parse it. Your 
solution is precisely what I was looking for, even if I didn't quite ask 
correctly. I appreciate the help!


-- Mike --

--
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree XML parsing problem

2011-04-27 Thread Mike

On 4/27/2011 12:33 PM, Hegedüs Ervin wrote:

hello,


I'm using ElementTree to parse an XML file, but it stops at the
second record (id = 002), which contains a non-standard ascii
character, ä. Here's the XML:










The complaint offered up by the parser is


I've checked this xml with your script, I think your locales
settings are not good.

$ ./parse.py

XML file: test.xml
001 High School
002 Universität Bremen
003 River College

(name of xml file is "test.xml")

So, I started change the codepage mark of xml:

  - same result
  - same result
  - same result

and then:
  - gives same error as you
described.

Try to change XML encoding.


a.


Thanks, Hegedüs and everyone else who responded. That is exactly it - 
I'm afraid I probably missed it in the docs because I was searching for 
terms like "unicode" and "coerce." In any event, that solves the 
problem. Thanks!


-- Mike --


--
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree XML parsing problem

2011-04-27 Thread Hegedüs Ervin
hello,

> I'm using ElementTree to parse an XML file, but it stops at the
> second record (id = 002), which contains a non-standard ascii
> character, ä. Here's the XML:
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> The complaint offered up by the parser is

I've checked this xml with your script, I think your locales
settings are not good.

$ ./parse.py 

XML file: test.xml
001 High School
002 Universität Bremen
003 River College

(name of xml file is "test.xml")

So, I started change the codepage mark of xml:

 - same result
 - same result
 - same result

and then:
 - gives same error as you
described.

Try to change XML encoding.


a.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree XML parsing problem

2011-04-27 Thread Philip Semanchuk

On Apr 27, 2011, at 2:26 PM, Mike wrote:

> I'm using ElementTree to parse an XML file, but it stops at the second record 
> (id = 002), which contains a non-standard ascii character, ä. Here's the XML:
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> The complaint offered up by the parser is
> 
> Unexpected error opening simple_fail.xml: not well-formed (invalid token): 
> line 5, column 40

You've gotten a number of good observations & suggestions already. I would add 
that if you're saving your XML file from a text editor, make sure you're saving 
it as UTF-8 and not ISO-8859-1 or Win-1252. 


bye
Philip

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree XML parsing problem

2011-04-27 Thread Neil Cerutti
On 2011-04-27, Mike  wrote:
> I'm using ElementTree to parse an XML file, but it stops at the
> second record (id = 002), which contains a non-standard ascii
> character, ?. Here's the XML:
>
>
>
>
>
>
>
>
>
>
> The complaint offered up by the parser is
>
> Unexpected error opening simple_fail.xml: not well-formed
> (invalid token): line 5, column 40

It seems to be an invalid XML document, as another poster
indicated.

> and if I change the line to eliminate the ?, everything is
> wonderful. The parser is perfectly happy with this
> modification:
>
> 
>
> I can't find anything in the ElementTree docs about allowing
> additional text characters or coercing strange ascii to
> Unicode.

If you're not the one generating that bogus file, then you can
specify the encoding yourself instead by declaring an XMLParser.

  import xml.etree.ElementTree as etree
  with open('file.xml') as xml_file:
parser = etree.XMLParser(encoding='ISO-8859-1')
root = etree.parse(xml_file, parser=parser).getroot()

-- 
Neil Cerutti
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree XML parsing problem

2011-04-27 Thread Benjamin Kaplan
On Wed, Apr 27, 2011 at 2:26 PM, Mike  wrote:
> I'm using ElementTree to parse an XML file, but it stops at the second
> record (id = 002), which contains a non-standard ascii character, ä. Here's
> the XML:
>
> 
> 
> 
> 
> 
> 
> 
> 
>
> The complaint offered up by the parser is
>
> Unexpected error opening simple_fail.xml: not well-formed (invalid token):
> line 5, column 40
>
> and if I change the line to eliminate the ä, everything is wonderful. The
> parser is perfectly happy with this modification:
>
> 
>
> I can't find anything in the ElementTree docs about allowing additional text
> characters or coercing strange ascii to Unicode.
>
> Is there a way to coerce the text so it doesn't cause the parser to raise an
> exception?
>

Have you tried specifying the file encoding? ä is not "strange ascii".
It's outside the ASCII range so if the parser expects ASCII, it will
get confused.

> Here's my test script (simple_fail contains the offending line, and
> simple_pass contains the line that passes).
>
> import sys
> import xml.etree.ElementTree as ET
>
> def main():
>
>    xml_files = ['simple_fail.xml', 'simple_pass.xml']
>    for xml_file in xml_files:
>
>        print
>        print 'XML file: %s' % (xml_file)
>
>        try:
>            tree = ET.parse(xml_file)
>        except Exception, inst:
>            print "Unexpected error opening %s: %s" % (xml_file, inst)
>            continue
>
>        root = tree.getroot()
>        records = root.find('records')
>        for record in records:
>            print record.attrib['id'], record.attrib['education']
>
> if __name__ == "__main__":
>        main()
>
>
> Thanks,
>
> -- Mike --
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


ElementTree XML parsing problem

2011-04-27 Thread Mike
I'm using ElementTree to parse an XML file, but it stops at the second 
record (id = 002), which contains a non-standard ascii character, ä. 
Here's the XML:











The complaint offered up by the parser is

Unexpected error opening simple_fail.xml: not well-formed (invalid 
token): line 5, column 40


and if I change the line to eliminate the ä, everything is wonderful. 
The parser is perfectly happy with this modification:




I can't find anything in the ElementTree docs about allowing additional 
text characters or coercing strange ascii to Unicode.


Is there a way to coerce the text so it doesn't cause the parser to 
raise an exception?


Here's my test script (simple_fail contains the offending line, and 
simple_pass contains the line that passes).


import sys
import xml.etree.ElementTree as ET

def main():

xml_files = ['simple_fail.xml', 'simple_pass.xml']
for xml_file in xml_files:

print
print 'XML file: %s' % (xml_file)

try:
tree = ET.parse(xml_file)
except Exception, inst:
print "Unexpected error opening %s: %s" % (xml_file, inst)
continue

root = tree.getroot()
records = root.find('records')
for record in records:
print record.attrib['id'], record.attrib['education']

if __name__ == "__main__":
main()


Thanks,

-- Mike --

--
http://mail.python.org/mailman/listinfo/python-list


Dealing with xml namespaces with ElementTree

2011-01-21 Thread Neil Cerutti
I have to parse many xml documents that senselessly(?) specify a
single namespace for the whole document. After a couple of years,
my approach has boiled down to the following three little
helpers, for use with ElementTree:

def insert_namespace(xpath):
# Enable *simple* xpath searches by inserting the fscking namespace.
return '/'.join('{{{}}}{}'.format(XMLNS, n) for n in xpath.split('/'))

def find(et, xpath):
return et.find(insert_namespace(xpath))

def findall(et, xpath):
return et.findall(insert_namespace(xpath))

Instead of writing, e.g.,
et.find('{{0}}ab/{{0}}cd'.format(XMLNS), et al, I can use
find(et, 'ab/cd').

Is there a better ElemenTree based approach I'm missing out on?
And on the other hand, am I circumventing something important, or
inviting bad limitations of some kind?

-- 
Neil Cerutti
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: no line breaks in xml file with elementTree

2010-10-31 Thread Diez B. Roggisch
hackingKK  writes:

> On Sunday 31 October 2010 01:58 PM, Lawrence D'Oliveiro wrote:
>> In message, hackingKK
>> wrote:
>>
>>
>>> I want to know if there is a way to have the ElementTree module write to
>>> an xml file with line breaks?
>>>  
>> Why does it matter? The XML files you generate are not for humans to look
>> at, are they?
>>
>
> So is there a function to generate tags with namespace?

http://lmgtfy.com/?q=element+tree+namespac

--

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: no line breaks in xml file with elementTree

2010-10-31 Thread Stefan Behnel

hackingKK, 31.10.2010 10:04:

On Sunday 31 October 2010 01:58 PM, Lawrence D'Oliveiro wrote:

hackingKK wrote:

Further more, I just was curious why elementtree is not having the
namespace facility?


ElementTree handles namespaces just fine.


So is there a function to generate tags with namespace?


Yes, it's called "Element", as in

el = ET.Element('{http://the.name/space}tag')

Reading the docs helps a lot here.

Stefan

--
http://mail.python.org/mailman/listinfo/python-list


Re: no line breaks in xml file with elementTree

2010-10-31 Thread hackingKK

On Sunday 31 October 2010 01:58 PM, Lawrence D'Oliveiro wrote:

In message, hackingKK
wrote:

   

I want to know if there is a way to have the ElementTree module write to
an xml file with line breaks?
 

Why does it matter? The XML files you generate are not for humans to look
at, are they?
   


So is there a function to generate tags with namespace?

happy hacking.
Krishnakant.

--
http://mail.python.org/mailman/listinfo/python-list


Re: no line breaks in xml file with elementTree

2010-10-31 Thread Lawrence D'Oliveiro
In message , hackingKK 
wrote:

> I want to know if there is a way to have the ElementTree module write to
> an xml file with line breaks?

Why does it matter? The XML files you generate are not for humans to look 
at, are they?

> Further more, I just was curious why elementtree is not having the
> namespace facility?

ElementTree handles namespaces just fine.
-- 
http://mail.python.org/mailman/listinfo/python-list


no line breaks in xml file with elementTree

2010-10-31 Thread hackingKK

Hello all.
I want to know if there is a way to have the ElementTree module write to 
an xml file with line breaks?
I find that when I use the write function from the module on a tree 
object, the resulting file has no line breaks.  I don't want to use 
prittyprint because it is adding extra tabs to the file and they are 
either counted as extra nodes or in case of element.text with 
elementtree, they are rendered as a part of the text, both being 
impractical results.
So is there a middle approach where we can have some  thing similar to 
prityprint but without the extra tabs and yet having a properly line 
breaked xml file?
Further more, I just was curious why elementtree is not having the 
namespace facility?

xml.dom.minidom has a way to generate tags with namespaces.
Any thing similar in elementTree?

happy hacking.
Krishnakant.



--
http://mail.python.org/mailman/listinfo/python-list


Re: is elementTree really a part of python 2.6?

2010-10-28 Thread Chris Rebert
On Thu, Oct 28, 2010 at 1:23 AM, hackingKK  wrote:
> Hello all,
> Some days back I had asked a few questions about parsing xml files using
> python.
> I have tryed dom.minidom module but I did not like the prittyPrint way of
> writing nodes.
> There were many other things I did not quite like about dom.minidom, so now
> trying to use elementTree.
> But to my surprise, I could not import xml.elementtree in my default python
> 2.6 setup.

The module is called xml.etree.ElementTree, *not* xml.elementtree.
It's been in Python since v2.5.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: is elementTree really a part of python 2.6?

2010-10-28 Thread Godson Gera
do the following

import xml.etree

that imports the elementtree library in 2.5 it self so you sure can do that
in 2.6

http://docs.python.org/library/xml.etree.elementtree.html

-- 
Thanks & Regards,
Godson Gera
Python Consultant
India<http://blog.godson.in/2010/09/how-to-make-python-xmlrpclib-client.html>

On Thu, Oct 28, 2010 at 1:53 PM, hackingKK  wrote:

>
> Hello all,
> Some days back I had asked a few questions about parsing xml files using
> python.
> I have tryed dom.minidom module but I did not like the prittyPrint way of
> writing nodes.
> There were many other things I did not quite like about dom.minidom, so now
> trying to use elementTree.
> But to my surprise, I could not import xml.elementtree in my default python
> 2.6 setup.
> is it possible that I have broken some thing?
> I had to later on do an easy_install for elementree.
> But I hear that elementtree is a part of python?
> So has my import gone wrong?  or is some thing broken in my python setup?
>
> happy hacking.
> Krishnakant.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Thanks & Regards,
Godson Gera
-- 
http://mail.python.org/mailman/listinfo/python-list


is elementTree really a part of python 2.6?

2010-10-28 Thread hackingKK


Hello all,
Some days back I had asked a few questions about parsing xml files using 
python.
I have tryed dom.minidom module but I did not like the prittyPrint way 
of writing nodes.
There were many other things I did not quite like about dom.minidom, so 
now trying to use elementTree.
But to my surprise, I could not import xml.elementtree in my default 
python 2.6 setup.

is it possible that I have broken some thing?
I had to later on do an easy_install for elementree.
But I hear that elementtree is a part of python?
So has my import gone wrong?  or is some thing broken in my python setup?

happy hacking.
Krishnakant.


--
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree handling nested tag

2010-10-04 Thread Diez B. Roggisch
tekion  writes:

> On Oct 3, 2:09 pm, de...@web.de (Diez B. Roggisch) wrote:
>> tekion  writes:
>> > On Oct 2, 5:32 am, de...@web.de (Diez B. Roggisch) wrote:
>> >> tekion  writes:
>> >> > All,
>> >> > I have the following xml tag:
>> >> > 
>> >> > 
>> >> >       httpRequest
>> >> >       HTTP://cmd.wma.ibm.com:80/
>> >> >       GET
>> >> >       200
>> >> >    
>> >> > 
>>
>> >> > I am interested in:
>> >> >        httpRequest
>> >> >       HTTP://cmd.wma.ibm.com:80/
>> >> >       GET
>> >> >       200
>> >> > as well as the upper layer tag. How do I get at the nest tag listed
>> >> > above?  Thanks.
>>
>> >> What is the "upper layer tag"? And what do you actually want to "get"?
>> >> The text-values? Or do you want to just create a document that just
>> >> consists of the resource_access tag?
>>
>> >> Then this should help:
>>
>> >> from xml.etree.ElementTree import *
>>
>> >> doc = """
>> >> 
>> >> 
>> >>       httpRequest
>> >>       HTTP://cmd.wma.ibm.com:80/
>> >>       GET
>> >>       200
>> >>    
>> >> 
>> >> """
>>
>> >> doc = fromstring(doc)
>>
>> >> resource_access = doc.find("resource_access")
>> >> print tostring(resource_access)
>>
>> >> Diez
>>
>> > Diez,
>> > This is the sample format from the doc. I the whole log file has this
>> > xml formated beginning and ending in the event tag. Is this something
>> > ElemenTtree can handle or is it better to user XSLT?  Thanks.
>>
>> Handle *what*? Can it read it? Yes. Can it extract data from it?
>> Yes. You still haven't said what you actually *want* with all this.
>>
>> Diez
>
> I wan to get the value of these tags:
>
> HTTP://cmd.wma.ibm.com:80/
> GET
> 200


from xml.etree.ElementTree import *

doc = """


  httpRequest
  HTTP://cmd.wma.ibm.com:80/
  GET
  200
   

"""


doc = fromstring(doc)

for resource_access in doc.findall("resource_access"):
print resource_access.find("httpurl").text

>
> You asked for what the upper layer tags are.  The upper layer tags I
> am referring for below tags are any tag that is above it.
>
> HTTP://cmd.wma.ibm.com:80/
> GET
> 200
>
> IE, for the upper upper are: httpRequest and 
> tags.

That's not true. action is on the same level as the others. And what you
you want to "get" from the upper layer tags?

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree handling nested tag

2010-10-03 Thread tekion
On Oct 3, 2:09 pm, de...@web.de (Diez B. Roggisch) wrote:
> tekion  writes:
> > On Oct 2, 5:32 am, de...@web.de (Diez B. Roggisch) wrote:
> >> tekion  writes:
> >> > All,
> >> > I have the following xml tag:
> >> > 
> >> > 
> >> >       httpRequest
> >> >       HTTP://cmd.wma.ibm.com:80/
> >> >       GET
> >> >       200
> >> >    
> >> > 
>
> >> > I am interested in:
> >> >        httpRequest
> >> >       HTTP://cmd.wma.ibm.com:80/
> >> >       GET
> >> >       200
> >> > as well as the upper layer tag. How do I get at the nest tag listed
> >> > above?  Thanks.
>
> >> What is the "upper layer tag"? And what do you actually want to "get"?
> >> The text-values? Or do you want to just create a document that just
> >> consists of the resource_access tag?
>
> >> Then this should help:
>
> >> from xml.etree.ElementTree import *
>
> >> doc = """
> >> 
> >> 
> >>       httpRequest
> >>       HTTP://cmd.wma.ibm.com:80/
> >>       GET
> >>       200
> >>    
> >> 
> >> """
>
> >> doc = fromstring(doc)
>
> >> resource_access = doc.find("resource_access")
> >> print tostring(resource_access)
>
> >> Diez
>
> > Diez,
> > This is the sample format from the doc. I the whole log file has this
> > xml formated beginning and ending in the event tag. Is this something
> > ElemenTtree can handle or is it better to user XSLT?  Thanks.
>
> Handle *what*? Can it read it? Yes. Can it extract data from it?
> Yes. You still haven't said what you actually *want* with all this.
>
> Diez

I wan to get the value of these tags:

HTTP://cmd.wma.ibm.com:80/
GET
200

You asked for what the upper layer tags are.  The upper layer tags I
am referring for below tags are any tag that is above it.

HTTP://cmd.wma.ibm.com:80/
GET
200

IE, for the upper upper are: httpRequest and 
tags.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree handling nested tag

2010-10-03 Thread Diez B. Roggisch
tekion  writes:

> On Oct 2, 5:32 am, de...@web.de (Diez B. Roggisch) wrote:
>> tekion  writes:
>> > All,
>> > I have the following xml tag:
>> > 
>> > 
>> >       httpRequest
>> >       HTTP://cmd.wma.ibm.com:80/
>> >       GET
>> >       200
>> >    
>> > 
>>
>> > I am interested in:
>> >        httpRequest
>> >       HTTP://cmd.wma.ibm.com:80/
>> >       GET
>> >       200
>> > as well as the upper layer tag. How do I get at the nest tag listed
>> > above?  Thanks.
>>
>> What is the "upper layer tag"? And what do you actually want to "get"?
>> The text-values? Or do you want to just create a document that just
>> consists of the resource_access tag?
>>
>> Then this should help:
>>
>> from xml.etree.ElementTree import *
>>
>> doc = """
>> 
>> 
>>       httpRequest
>>       HTTP://cmd.wma.ibm.com:80/
>>       GET
>>       200
>>    
>> 
>> """
>>
>> doc = fromstring(doc)
>>
>> resource_access = doc.find("resource_access")
>> print tostring(resource_access)
>>
>> Diez
>
> Diez,
> This is the sample format from the doc. I the whole log file has this
> xml formated beginning and ending in the event tag. Is this something
> ElemenTtree can handle or is it better to user XSLT?  Thanks.

Handle *what*? Can it read it? Yes. Can it extract data from it?
Yes. You still haven't said what you actually *want* with all this.

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree handling nested tag

2010-10-03 Thread tekion
On Oct 2, 5:32 am, de...@web.de (Diez B. Roggisch) wrote:
> tekion  writes:
> > All,
> > I have the following xml tag:
> > 
> > 
> >       httpRequest
> >       HTTP://cmd.wma.ibm.com:80/
> >       GET
> >       200
> >    
> > 
>
> > I am interested in:
> >        httpRequest
> >       HTTP://cmd.wma.ibm.com:80/
> >       GET
> >       200
> > as well as the upper layer tag. How do I get at the nest tag listed
> > above?  Thanks.
>
> What is the "upper layer tag"? And what do you actually want to "get"?
> The text-values? Or do you want to just create a document that just
> consists of the resource_access tag?
>
> Then this should help:
>
> from xml.etree.ElementTree import *
>
> doc = """
> 
> 
>       httpRequest
>       HTTP://cmd.wma.ibm.com:80/
>       GET
>       200
>    
> 
> """
>
> doc = fromstring(doc)
>
> resource_access = doc.find("resource_access")
> print tostring(resource_access)
>
> Diez

Diez,
This is the sample format from the doc. I the whole log file has this
xml formated beginning and ending in the event tag. Is this something
ElemenTtree can handle or is it better to user XSLT?  Thanks.

  2005-10-02-22:01:36.187-04:00I-
  1
  
http
109
1
cmd.wma.ibm.com
  
  
Unauthenticated
9.54.83.206
IPV4
  
  
/
HTTP://cmd.wma.ibm.com:80/
  
  
httpRequest
HTTP://cmd.wma.ibm.com:80/
GET
200
  
  
GET HTTP://cmd.wma.ibm.com:80/ HTTP/1.0
1970
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
  


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree handling nested tag

2010-10-02 Thread Diez B. Roggisch
tekion  writes:

> All,
> I have the following xml tag:
> 
> 
>   httpRequest
>   HTTP://cmd.wma.ibm.com:80/
>   GET
>   200
>
> 
>
> I am interested in:
>httpRequest
>   HTTP://cmd.wma.ibm.com:80/
>   GET
>   200
> as well as the upper layer tag. How do I get at the nest tag listed
> above?  Thanks.

What is the "upper layer tag"? And what do you actually want to "get"?
The text-values? Or do you want to just create a document that just
consists of the resource_access tag?

Then this should help:


from xml.etree.ElementTree import *

doc = """


  httpRequest
  HTTP://cmd.wma.ibm.com:80/
  GET
  200
   

"""


doc = fromstring(doc)

resource_access = doc.find("resource_access")
print tostring(resource_access)

Diez

-- 
http://mail.python.org/mailman/listinfo/python-list


ElementTree handling nested tag

2010-10-01 Thread tekion
All,
I have the following xml tag:


  httpRequest
  HTTP://cmd.wma.ibm.com:80/
  GET
  200
   


I am interested in:
   httpRequest
  HTTP://cmd.wma.ibm.com:80/
  GET
  200
as well as the upper layer tag. How do I get at the nest tag listed
above?  Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can't find elements using ElementTree find method

2010-08-31 Thread Brendan Simon (eTRIX)
 I can't use getroot() when using fromstring() -- as fromstring()
returns an Element, not an ElementTree object.

Yes, my root is the 'components' element, but find() seems to insist on
searching for sub-elements.

Ideally, I would like root.find('components') or
root.find('./components') to find the components element at the top-level.

I'd also like to be able to do root.find('./components/component') or
root.find('./components/component/name') and so on.

I guess I just have to check that root.tag == 'components' first, then
search for './component', './component/name' and so on.  It's a bit
ugly, but heaps better than using minidom :)

Cheers, Brendan.


On 31/08/10 6:57 PM, Nitin Pawar wrote:
> Try using getroot()
>
> I think your root is components so its searching in root 
>
> On Tue, Aug 31, 2010 at 2:19 PM, Brendan Simon (eTRIX)
> mailto:brendan.si...@etrix.com.au>> wrote:
>
> I am trying to use ElementTree (with Python 2.7) and can't seem to
> find elements at the top level.  The find() and findall() methods
> seem to find elements within the top level, but not if it the
> elements are at the top level.
>
> How do I find top level elements ??
> Here is my code.
>
> import xml.etree.ElementTree as ET
>
> xml = '''\
> 
> 
>   
> Fred
> Australia
>   
> 
> '''
>
> root = ET.fromstring( xml )
>
> ### This pattern is not found :(
> comps = root.find( './/components' )
>
> ### These patterns are found ok :)
> comp = root.find( './/component' )
> name = root.find( './/name' )
>
> print 'comps =', comps
> print 'comp =', comp
> print 'name =', name
>
>
> Thanks, Brendan.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
>
>
> -- 
> Nitin Pawar
>

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can't find elements using ElementTree find method

2010-08-31 Thread Stefan Behnel

Brendan Simon (eTRIX), 31.08.2010 10:49:

  I am trying to use ElementTree (with Python 2.7) and can't seem to find
elements at the top level.  The find() and findall() methods seem to
find elements within the top level, but not if it the elements are at
the top level.

How do I find top level elements ??
Here is my code.

 import xml.etree.ElementTree as ET

 xml = '''\
 
 
   
 Fred
 Australia
   
 
 '''

 root = ET.fromstring( xml )

 ### This pattern is not found :(
 comps = root.find( './/components' )


"." matches the current element, so the path expression looks for all 
"components" nodes *below* the current element.


You can either wrap the root in an ElementTree and search globally (i.e. 
without the leading "."), or you can test the root node yourself.


Stefan

--
http://mail.python.org/mailman/listinfo/python-list


Re: Can't find elements using ElementTree find method

2010-08-31 Thread Nitin Pawar
Try using 
getroot()<http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree.getroot>

I think your root is components so its searching in root

On Tue, Aug 31, 2010 at 2:19 PM, Brendan Simon (eTRIX) <
brendan.si...@etrix.com.au> wrote:

>  I am trying to use ElementTree (with Python 2.7) and can't seem to find
> elements at the top level.  The find() and findall() methods seem to find
> elements within the top level, but not if it the elements are at the top
> level.
>
> How do I find top level elements ??
> Here is my code.
>
> import xml.etree.ElementTree as ET
>
> xml = '''\
> 
> 
>   
> Fred
> Australia
>   
> 
> '''
>
> root = ET.fromstring( xml )
>
> ### This pattern is not found :(
> comps = root.find( './/components' )
>
> ### These patterns are found ok :)
> comp = root.find( './/component' )
> name = root.find( './/name' )
>
> print 'comps =', comps
> print 'comp =', comp
> print 'name =', name
>
>
> Thanks, Brendan.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


-- 
Nitin Pawar
-- 
http://mail.python.org/mailman/listinfo/python-list


Can't find elements using ElementTree find method

2010-08-31 Thread Brendan Simon (eTRIX)
 I am trying to use ElementTree (with Python 2.7) and can't seem to find
elements at the top level.  The find() and findall() methods seem to
find elements within the top level, but not if it the elements are at
the top level.

How do I find top level elements ??
Here is my code.

import xml.etree.ElementTree as ET

xml = '''\


  
Fred
Australia
  

'''

root = ET.fromstring( xml )

### This pattern is not found :(
comps = root.find( './/components' )

### These patterns are found ok :)
comp = root.find( './/component' )
name = root.find( './/name' )

print 'comps =', comps
print 'comp =', comp
print 'name =', name


Thanks, Brendan.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using elementtree to Create HTML Form / Set "selected"

2010-08-12 Thread Doug
On Aug 12, 10:47 am, Peter Otten <__pete...@web.de> wrote:
> Doug wrote:
> > I'm using elementtree to create a form.
>
> > I would like to set the "selected" attribute.
>
> > Setting using the usual
> >  option.set( "selected" = "" )
>
> Maybe that should be option.set(selected="selected"). I think
>
> 
>
> and
>
> 
>
> are equivalent.
>
> http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2
>
>
>
> > gives me
> >   Operations
> > how does one make
> >   Operations
> > which is what I need.

Makes sense to me. Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using elementtree to Create HTML Form / Set "selected"

2010-08-12 Thread Peter Otten
Doug wrote:

> I'm using elementtree to create a form.
> 
> I would like to set the "selected" attribute.
> 
> Setting using the usual
>  option.set( "selected" = "" )

Maybe that should be option.set(selected="selected"). I think



and



are equivalent.

http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2

> gives me
>   Operations
> how does one make
>   Operations
> which is what I need.

-- 
http://mail.python.org/mailman/listinfo/python-list


Using elementtree to Create HTML Form / Set "selected"

2010-08-12 Thread Doug
I'm using elementtree to create a form.

I would like to set the "selected" attribute.

Setting using the usual
 option.set( "selected" = "" )
gives me
  Operations
how does one make
  Operations
which is what I need.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is ElementTree development still active?

2010-08-11 Thread Terry Reedy

On 8/11/2010 12:21 PM, Pinku Surana wrote:

I checked the svn repo at effbot.org, but it appears to have no
updates since 2007. Has development moved elsewhere?


It is now in the stdlib (19.11) as xml.etree.ElementTree
http://svn.python.org/view/python/branches/py3k/Modules/_elementtree.c?revision=83949&view=markup
last revised 12 min ago.

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Is ElementTree development still active?

2010-08-11 Thread Pinku Surana
I checked the svn repo at effbot.org, but it appears to have no
updates since 2007. Has development moved elsewhere?

Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with Elementtree and XMLSchem instance type

2010-08-07 Thread Stefan Behnel

Roland Hedberg, 30.07.2010 15:21:

I have the following XML snippet:

http://www.w3.org/2001/XMLSchema-instance";
 xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706";
 xsi:type="fed:SecurityTokenServiceType">
 


This part after parsing with Elementtree gives me an Element instance
with the following properties:


tree.tag

{urn:oasis:names:tc:SAML:2.0:metadata}RoleDescriptor

tree.keys()

['{http://www.w3.org/2001/XMLSchema-instance}type']

tree['{http://www.w3.org/2001/XMLSchema-instance}type']

fed:SecurityTokenServiceType

And there is the problem, I've lost the coupling between the prefix
'fed' and the namespace
"http://docs.oasis-open.org/wsfed/federation/200706";.

Is there any way I can get at the prefix<->  namespace mapping from an
Element instance ?
I've read the documentation I can find and there is nothing that tells
me how to get at the mapping.

If I print the Element instance the prefix 'fed' is replace by 'ns0' or
something like that.
Definitely something that has no connection to the original 'fed'.


Yes, ElementTree does that. There are two ways to work around this: recent 
ET versions support explicitly defining namespace-prefix mappings at a 
module global level (IIRC, starting with ET 1.3/Py2.7/Py3.2), so you can 
provide a fixed prefix if you know what's coming in.


Failing that, you can switch to lxml.etree, which keeps namespace prefixes 
as seen in the parsed document.


Stefan

--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with Elementtree and XMLSchem instance type

2010-08-02 Thread Lawrence D'Oliveiro
In message
<6627204c-d0b1-456d-94bd-76d946ad2...@g6g2000pro.googlegroups.com>, Carl 
Banks wrote:

> On Aug 1, 5:43 pm, Lawrence D'Oliveiro 
> wrote:
>
>> In message
>> <96e47fd8-c939-48a2-9a2b-92afa720c...@k1g2000prl.googlegroups.com>, Carl
>> Banks wrote:
>>
>>> My general feeling is that ElementTree is a lot handier for reading
>>> and writing your own XML formats, than for handling XML files produced
>>> by other tools.
>>
>> Why is that? I’ve successfully used it to parse SVG files produced by
>> Inkscape <http://github.com/ldo/dvd_menu_animator>.
> 
> I said it was not handy, not that is was not useful.

If you want to split hairs, I didn’t say it was “useful”, and I certainly 
found it very “handy” for that purpose.

> And you don't *have* to try to start an argument over every tiny thing
> you disagree with.

What else is USENET for? :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with Elementtree and XMLSchem instance type

2010-08-02 Thread NewBlue
> "Lawrence" == Lawrence D'Oliveiro  
> writes:

Lawrence> In message
Lawrence> ,
Lawrence> Roland
Lawrence> Hedberg wrote:

> And there is the problem, I've lost the coupling between the prefix
>> 'fed' and the namespace
>> "http://docs.oasis-open.org/wsfed/federation/200706";.

Lawrence> Why is this a problem?
我在设立
-- 
自从你离开了以后,我一直等待着你回来
世人笑我懵懂,我笑世人不懂
My homepage:[http://211.92.88.40/~newblue]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with Elementtree and XMLSchem instance type

2010-08-01 Thread Carl Banks
On Aug 1, 5:43 pm, Lawrence D'Oliveiro  wrote:
> In message
> <96e47fd8-c939-48a2-9a2b-92afa720c...@k1g2000prl.googlegroups.com>, Carl
>
> Banks wrote:
> > My general feeling is that ElementTree is a lot handier for reading
> > and writing your own XML formats, than for handling XML files produced
> > by other tools.
>
> Why is that? I’ve successfully used it to parse SVG files produced by
> Inkscape <http://github.com/ldo/dvd_menu_animator>.

I said it was not handy, not that is was not useful.

And you don't *have* to try to start an argument over every tiny thing
you disagree with.


Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with Elementtree and XMLSchem instance type

2010-08-01 Thread Lawrence D'Oliveiro
In message
<96e47fd8-c939-48a2-9a2b-92afa720c...@k1g2000prl.googlegroups.com>, Carl 
Banks wrote:

> My general feeling is that ElementTree is a lot handier for reading
> and writing your own XML formats, than for handling XML files produced
> by other tools.

Why is that? I’ve successfully used it to parse SVG files produced by 
Inkscape <http://github.com/ldo/dvd_menu_animator>.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with Elementtree and XMLSchem instance type

2010-07-31 Thread Carl Banks
On Jul 30, 6:21 am, Roland Hedberg  wrote:
> Hi!
>
> I have the following XML snippet:
>
>      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
>     xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706";
>     xsi:type="fed:SecurityTokenServiceType">
>     
> 
>
> This part after parsing with Elementtree gives me an Element instance
> with the following properties:
>
> > tree.tag
>
> {urn:oasis:names:tc:SAML:2.0:metadata}RoleDescriptor> tree.keys()
>
> ['{http://www.w3.org/2001/XMLSchema-instance}type']> 
> tree['{http://www.w3.org/2001/XMLSchema-instance}type']
>
> fed:SecurityTokenServiceType
>
> And there is the problem, I've lost the coupling between the prefix
> 'fed' and the namespace
> "http://docs.oasis-open.org/wsfed/federation/200706";.
>
> Is there any way I can get at the prefix <-> namespace mapping from an
> Element instance ?
> I've read the documentation I can find and there is nothing that tells
> me how to get at the mapping.


ElementTree doesn't have a way to do this.

My general feeling is that ElementTree is a lot handier for reading
and writing your own XML formats, than for handling XML files produced
by other tools.

The lxml package has an ElementTree like interface but with some
methods to handle namespace abbreviations.


Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with Elementtree and XMLSchem instance type

2010-07-31 Thread Lawrence D'Oliveiro
In message , Roland 
Hedberg wrote:

> And there is the problem, I've lost the coupling between the prefix
> 'fed' and the namespace
> "http://docs.oasis-open.org/wsfed/federation/200706";.

Why is this a problem?
-- 
http://mail.python.org/mailman/listinfo/python-list


Problem with Elementtree and XMLSchem instance type

2010-07-30 Thread Roland Hedberg
Hi!

I have the following XML snippet:

http://www.w3.org/2001/XMLSchema-instance";
xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706";
xsi:type="fed:SecurityTokenServiceType">



This part after parsing with Elementtree gives me an Element instance
with the following properties:

> tree.tag
{urn:oasis:names:tc:SAML:2.0:metadata}RoleDescriptor
> tree.keys()
['{http://www.w3.org/2001/XMLSchema-instance}type']
> tree['{http://www.w3.org/2001/XMLSchema-instance}type']
fed:SecurityTokenServiceType

And there is the problem, I've lost the coupling between the prefix
'fed' and the namespace
"http://docs.oasis-open.org/wsfed/federation/200706";.

Is there any way I can get at the prefix <-> namespace mapping from an
Element instance ?
I've read the documentation I can find and there is nothing that tells
me how to get at the mapping.

If I print the Element instance the prefix 'fed' is replace by 'ns0' or
something like that.
Definitely something that has no connection to the original 'fed'.

-- Roland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pretty printing with ElementTree

2010-07-09 Thread abhijeet thatte
It worked.

Thanks,
Abhijeet


On Fri, Jul 9, 2010 at 4:03 PM, John Krukoff  wrote:

> On Fri, 2010-07-09 at 15:46 -0700, abhijeet thatte wrote:
> > Hi,
> >
> >
> > Does any one know how to use pretty printing with ElementTree while
> > generating xml files.
> > We can use that with lxml. But I want to stick with it ElementTree.
> >
> >
> > Thanks,
> > Abhijeet
>
>
> It's pretty simple minded, but this recipe from the element tree
> documentation may do what you want:
> http://effbot.org/zone/element-lib.htm#prettyprint
>
> --
> John Krukoff 
> Land Title Guarantee Company
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pretty printing with ElementTree

2010-07-09 Thread John Krukoff
On Fri, 2010-07-09 at 15:46 -0700, abhijeet thatte wrote:
> Hi,
> 
> 
> Does any one know how to use pretty printing with ElementTree while
> generating xml files. 
> We can use that with lxml. But I want to stick with it ElementTree. 
> 
> 
> Thanks,
> Abhijeet


It's pretty simple minded, but this recipe from the element tree
documentation may do what you want:
http://effbot.org/zone/element-lib.htm#prettyprint

-- 
John Krukoff 
Land Title Guarantee Company

-- 
http://mail.python.org/mailman/listinfo/python-list


Pretty printing with ElementTree

2010-07-09 Thread abhijeet thatte
Hi,

Does any one know how to use pretty printing with ElementTree while
generating xml files.
We can use that with lxml. But I want to stick with it ElementTree.

Thanks,
Abhijeet
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree write creates large one line XML file ....

2010-05-28 Thread robert somerville
Thanks Robert Kern :

"prettyprint" ; indent()  does the trick ;-)


>ElementTree writes exactly what you tell it to. In XML, whitespace is
>significant. If you want newlines and/or indentation to make it pretty-looking,
>then you need to add those to your elements.
>
>Fredrik provides an example function for doing this:
>
>   http://effbot.org/zone/element-lib.htm#prettyprint
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree write creates large one line XML file ....

2010-05-27 Thread Sebastian Bassi
On Thu, May 27, 2010 at 9:13 PM, Robert Kern  wrote:
> ElementTree writes exactly what you tell it to. In XML, whitespace is
> significant. If you want newlines and/or indentation to make it
> pretty-looking, then you need to add those to your elements.

This is not always true. Let me quote an XML tutorial (by Oracle):


"What is XML Whitespace?
XML considers four characters to be whitespace: the carriage return
(\r or ch(13)), the linefeed (\n or ch(10)), the tab(\t), and the
spacebar (' '). In XML documents, there are two types of whitespace:

Significant whitespace is part of the document content and should be preserved.
Insignificant whitespace is used when editing XML documents for
readability. These whitespaces are typically not intended for
inclusion in the delivery of the document.

Usually without DTD or XML schema definition, all whitespaces are
significant whitespaces and should be preserved. However, with DTD or
XML schema definitions, only the whitespaces in the content are
significant as follows:


   --
   John Smith
   Product Manager
   Example.com
   

"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree write creates large one line XML file ....

2010-05-27 Thread Robert Kern

On 5/27/10 7:52 PM, robert somerville wrote:

Hi I am using Ubuntu 9.10 and Python 2.6.4 ..

when I create an ElementTree object and the write it out using:

  xml.etree.ElementTree.write() , I get one single long single line
files, instead of something that looks reasonable , what gives ??? (and
is it important ??)


ElementTree writes exactly what you tell it to. In XML, whitespace is 
significant. If you want newlines and/or indentation to make it pretty-looking, 
then you need to add those to your elements.


Fredrik provides an example function for doing this:

  http://effbot.org/zone/element-lib.htm#prettyprint

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list


ElementTree write creates large one line XML file ....

2010-05-27 Thread robert somerville
Hi I am using Ubuntu 9.10 and Python 2.6.4 ..

when I create an ElementTree object and the write it out using:

 xml.etree.ElementTree.write() , I get one single long single line files,
instead of something that looks reasonable , what gives ??? (and is it
important ??)

eg: I get :

OneTwo

instead of :


One
Two



i found this example at: ( Listing 4 )
http://www.learningpython.com/2008/05/07/elegant-xml-parsing-using-the-elementtree-module/#Listing1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Elementtree install problem in Ubuntu (a newbie ..)

2010-03-10 Thread Philip Semanchuk


On Mar 10, 2010, at 6:48 PM, robert somerville wrote:


Hi ;
I installed the elementtree and celementree packages throught the  
synaptic

package manager, all seems to go fine through the install ...

when i startup python and try to import them (as per the EFFBOTT.org
suggestions) .. PROBLEMS ... (see below ..) What am i doing  
wrong ??? this

is a new window after installation of packages ...

Python 2.6.4 (r264:75706, Dec  7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import elementtree.ElementTree as ET

Traceback (most recent call last):
 File "", line 1, in 
ImportError: No module named elementtree.ElementTree

import elementtree

Traceback (most recent call last):
 File "", line 1, in 
ImportError: No module named elementtree


Hi Robert,
In Python >= 2.5, ElementTree is part the standard library so it  
doesn't need to be installed separately. Here's the documentation for  
it:

http://www.python.org/doc/2.6.4/library/xml.etree.elementtree.html


HTH
Philip
--
http://mail.python.org/mailman/listinfo/python-list


Elementtree install problem in Ubuntu (a newbie ..)

2010-03-10 Thread robert somerville
Hi ;
I installed the elementtree and celementree packages throught the synaptic
package manager, all seems to go fine through the install ...

when i startup python and try to import them (as per the EFFBOTT.org
suggestions) .. PROBLEMS ... (see below ..) What am i doing wrong ??? this
is a new window after installation of packages ...

Python 2.6.4 (r264:75706, Dec  7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import elementtree.ElementTree as ET
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named elementtree.ElementTree
>>> import elementtree
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named elementtree


thanks;
Robert somerville
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   >