4

I'm trying to parse an XML document and get certain tags. I'd like to grab the name tag (only if it's the name tag nested within artist) and the title tag (only if it's the one nested within release).
That's not too important, though, the important thing is that I'm for some reason getting an error saying the elif statement is invalid syntax
I've looked through other posts and made sure that my tabbing is correct and that there aren't any extra newlines after any of the if's.

This is the code snippet:

from lxml import etree
import sys

#infile = raw_input("Please enter an XML file to parse:  ")
outfile = open('results.txt', 'a')

path = []
for event, elem in etree.iterparse('releases7.xml', events=("start", "end")):
    if event == 'start':
        path.append(elem.tag)
    elif event == 'end':
        # process the tag
        if elem.tag == 'name':
            if 'artist' in path and not 'extraartists' in path and not 'track' in path:
                outfile.write( 'artist = ' + elem.text.encode('utf-8') + '\n' )
        elif elem.tag == 'title':
            if 'release' in path and not 'track' in path:
                outfile.write( 'release title = ' + elem.text.encode('utf-8') + '\n')
            else:
                print 'nonrelease'
        path.pop()

This is the error:

File "DataDestroy_Fast.py", line 18
elif elem.tag == 'title':
   ^
SyntaxError: invalid syntax

(Note: Using Python2.7 on Mac OSX)

arc
  • 477
  • 2
  • 8
  • 14
  • `outfile.write( 'artist = ' + elem.text.encode('utf-8') + '\n' );` << does it suppose to have an extra `;` there? That jumps out to me... – George May 25 '13 at 02:05
  • oh, that was just put there because i wanted to see if it made any difference. it functions the same with or without – arc May 25 '13 at 02:06
  • 2
    at at the end of line 3 of your code snippet, at the end. There is a `;` – George May 25 '13 at 02:07
  • @arc I've run your code on my machine and it works ... – satoru May 25 '13 at 02:08
  • i'll post an image of all of my code... maybe it's something further up?? – arc May 25 '13 at 02:09
  • 3
    @arc: Post all of your actual code. Also, you're using the `print` statement, so you can't really tag this as 3.x. – Blender May 25 '13 at 02:15
  • done, and tag removed. – arc May 25 '13 at 02:19
  • @arc: Make sure to delete any `.pyc` files as well. Your code is fine. Can you make sure that you aren't running this with Python 3? – Blender May 25 '13 at 02:21
  • 6
    I had an error like this once that was due to a extra tab character that just happened to be right at a position where it did nothing visible. Can you view invisible characters or something in your editor? – martineau May 25 '13 at 02:23
  • invisible characters... they'll get ya. thanks! I had an invisible character in that line. – arc May 25 '13 at 02:32
  • 1
    @martineau: could you post that as an answer, so that this question no longer stays unanswered? – user1251007 Nov 19 '13 at 20:59

2 Answers2

6

As I mentioned in a comment, I had an error like this once that was due to a extra tab character that just happened to be right at a position where it did nothing visible.

If your editor will let you view invisible characters like tabs and newlines, you may be able to actually see if that the case. My editor also has an option to convert tabs to spaces which would fix such a problem. If all else fails, just delete all the whitespace at the beginning of the line and then carefully do it over and then see if the error still occurs.

Recently I ran across a really good answer to a similar question How can I add a print statement to this code without getting an indentation error.

Community
  • 1
  • 1
martineau
  • 119,623
  • 25
  • 170
  • 301
0

The if at the middle part, as shown below:

"if 'artist' in path and not 'extraartists' in path and not 'track' in path: outfile.write( 'artist = ' + elem.text.encode('utf-8') + '\n' )"

Should be closed with an else before you go back into "elif elem.tag == 'title':"

My advice is to create another if function instead of an if inside an if. It would be less complicated.

Disrudog
  • 59
  • 8