Total Nooob Help

This is the place for queries that don't fit in any of the other categories.

Total Nooob Help

Postby ylafont » Tue Apr 01, 2014 10:36 pm

I began to experiment with python yesterday since i was in need of parsing an xml file. After playing sed and awk, I was quickly given the advice to us python.

i have the following script

Code: Select all
#!/usr/bin/env python

import sys
import xml.etree.ElementTree as ET
tree = ET.parse("xmltv3.xml")
root = tree.getroot()

for Channel in root.iter('channel'):
   ChannelID = '.'.join(Channel.attrib.get('id').split('.')[0:3])
   
   Count = 0
   for ChannelInfo in Channel:
      if ChannelInfo.tag == 'display-name':
         Count += 1
   if Count == 5:
      print ChannelID, 'ONLY 5 FILEDS in record.'
      [b]#ChannelInfo.insert(3,'test')[/b]
   else:
      print ChannelID, '\t' .join (ChannelInfo.text for ChannelInfo in Channel,)


Which produces the following output.

Code: Select all
I5.1.28457893 5.1 WNYWDT        5.1     44 WNYWDT fcc   WNYWDT  WNYWDT (WNYW-DT)        Fox Affiliate
I5.2.41743441 5.2 WNYWDT2       5.2     44 WNYWDT2 fcc  WNYWDT2 WNYWDT2 (WNYW-DT2)      Independent
I7.1.28455323 ONLY 5 FILEDS in record.
I7.2.41103996 ONLY 5 FILEDS in record.
I9.1.43584439 9.1 WWORDT        9.1     38 WWORDT fcc   WWORDT  WWORDT (WWOR-DT)        MyNetworkTV Affiliate
I9.3.273073824 9.3 WWORDT3      9.3     38 WWORDT3 fcc  WWORDT3 WWORDT3 (WWOR-DT3)      Independent
I9.4.290647859 9.4 WWORDT4      9.4     38 WWORDT4 fcc  WWORDT4 WWORDT4 (WWOR-DT4)      MundoFOX Affiliate


my problem is when i try it insert a records with the line #ChannelInfo.insert(3,'test'), which bombs out with the following.

Code: Select all
Traceback (most recent call last):
  File "xmltv.py", line 8, in <module>
    for Channel in root.iter('channel'):
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 477, in iter
    for e in e.iter(tag):
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 477, in iter
    for e in e.iter(tag):
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 477, in iter
    for e in e.iter(tag):
AttributeError: 'str' object has no attribute 'iter'



What i am missing? if someone can point me in the right direction, please. thank you.
Last edited by stranac on Tue Apr 01, 2014 10:42 pm, edited 1 time in total.
Reason: Added code tags. Locked.
ylafont
 
Posts: 23
Joined: Tue Apr 01, 2014 10:29 pm

Re: Total Nooob Help

Postby snippsat » Wed Apr 02, 2014 6:02 am

Error message means that root is a string object and not xml.etree object.
Error happens in line 8,so i don't understand "line #ChannelInfo.insert(3,'test')" give the error.

I can make the same error message.
Code: Select all
>>> root = 'hello'
>>> root.iter
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: 'str' object has no attribute 'iter'

A test version.
Code: Select all
import xml.etree.ElementTree as ET
tree = ET.parse("test.xml")
root = tree.getroot()

Test.
Code: Select all
>>> root
<Element 'note' at 0x349c0d0>
>>> type(root) #Not a string object
<class 'xml.etree.ElementTree.Element'>
>>> root.iter
<bound method Element.iter of <Element 'note' at 0x349c0d0>>

>>> for element in root.iter('to'):
...     print element.text
...     
one
two
User avatar
snippsat
 
Posts: 231
Joined: Thu Feb 21, 2013 12:04 am

Re: Total Nooob Help

Postby stranac » Wed Apr 02, 2014 9:19 am

snippsat wrote:Error message means that root is a string object and not xml.etree object.

Not really, it means something in the tree is a string, which is not surprising, since the OP is inserting a string into the tree.
If you take a closer look at his code, it's obvious that root is an Element object.

ylafont:
If you want to add new elements to a tree, they have to be actual Elements.
Inserting a string won't work.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1152
Joined: Thu Feb 07, 2013 3:42 pm

Re: Total Nooob Help

Postby ylafont » Wed Apr 02, 2014 5:37 pm

stranac, can you elaborate on the syntax for the line, I think i tried every variation, including

ChannelInfo.Element(insert(3,'text'))

unfortunately, the documentation is not meant for a noob, One really needs to go through trial and error in order to understand it. at least me anyway.
ylafont
 
Posts: 23
Joined: Tue Apr 01, 2014 10:29 pm

Re: Total Nooob Help

Postby stranac » Wed Apr 02, 2014 7:37 pm

What do you want that line to do exactly?
A small example is also welcome.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1152
Joined: Thu Feb 07, 2013 3:42 pm

Re: Total Nooob Help

Postby ylafont » Wed Apr 02, 2014 8:42 pm

Here is a sample my source file.

Code: Select all
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE tv SYSTEM "xmltv.dtd">

<tv source-info-name="Zap2it" generator-info-name="mc2xml" generator-info-url="mailto:mc2xml@gmail.com">
   <channel id="I2.1.28459048.microsoft.com">
      <display-name>2.1 WCBSDT</display-name>
      <display-name>2.1</display-name>
      <display-name>WCBSDT</display-name>
      <display-name>WCBSDT (WCBS-DT)</display-name>
      <display-name>CBS Affiliate</display-name>
   </channel>
   <channel id="I2.2.272674196.microsoft.com">
      <display-name>2.2 WCBSDT2</display-name>
      <display-name>2.2</display-name>
      <display-name>33 WCBSDT2 fcc</display-name>
      <display-name>WCBSDT2</display-name>
      <display-name>WCBSDT2 (WCBS-DT2)</display-name>
      <display-name>Independent</display-name>
   </channel>
</tv>


Some of the <display-name> elements only have 5 entries, which is why i count in them in the for loop and insert one on the third line if it is missing.

Code: Select all
for Channel in root.iter('channel'):
   ChannelID = '.'.join(Channel.attrib.get('id').split('.')[0:3])
   
   Count = 0
   for ChannelInfo in Channel:
      if ChannelInfo.tag == 'display-name':
         Count += 1
   if Count == 5:
      print ChannelID, 'ONLY 5 FILEDS in record.'
      ChannelInfo.insert(3,'test')  # <<<<<------------ Insert new  element and print with a tab delimiter
      print ChannelID, '\t' .join (ChannelInfo.text for ChannelInfo in Channel,)
   else:
      print ChannelID, '\t' .join (ChannelInfo.text for ChannelInfo in Channel,)


hopefully i can understand it better once I see your line inserting the element. thank you.
Last edited by stranac on Wed Apr 02, 2014 8:48 pm, edited 1 time in total.
Reason: Added code tags.
ylafont
 
Posts: 23
Joined: Tue Apr 01, 2014 10:29 pm

Re: Total Nooob Help

Postby stranac » Wed Apr 02, 2014 9:14 pm

You need to read this to learn how to post properly: viewtopic.php?t=145

So you want to insert a new 'display-name' element?
Something like this should work(assuming you imported xml.etree.ElementTree as ET):
Code: Select all
new_element = ET.Element('display-name')
new_element.text = 'some text'
Channel.insert(2, new_element)

A couple of notes: you want to insert the new tag into the channel element, and to insert something as a third item, you should use the index 2.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1152
Joined: Thu Feb 07, 2013 3:42 pm

Re: Total Nooob Help

Postby ylafont » Wed Apr 02, 2014 9:33 pm

Never in million years would it have occurred to me to define variables for the name and value of the element before inserting it.

thank you, That gives a reference point on structure for python.

... will read the Newbie Section. thank you again.
ylafont
 
Posts: 23
Joined: Tue Apr 01, 2014 10:29 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests