-3

I have just started learning Python, and the indention is giving me lots of headache (previously I always used languages that separate blocks by braces).

I have switched many IDEs, read many tips how to avoid the indention issue, but can't understand what mistake I am having in below code that is causing "inconsistent use of tab and space ......".

Basically I am just trying to store all the text inside "mx" tags (in an xml file) in an array "fields".

import xml.etree.ElementTree as ET

tree = ET.parse('C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\wparse\data\test1.xml')

root = tree.getroot()

dicto = {}

fields = []
for device in root.iter("main_node"):
    moid = device.find("subnode1").find("subnode2").find("id").text
    if "valid" in moid and "ignore" not in moid:
        for x in device.find("subnode1").findall("mx"):
            fields.append(x.text)

print(fields)
abdfahim
  • 2,523
  • 10
  • 35
  • 71
  • 1
    Don't mix the two. Stick to spaces perhaps. – devnull Apr 30 '14 at 07:11
  • 1
    More specifically, configure your editor to use four spaces for each level of indentation (as per [PEP-8 (Style Guide for Python code)](http://www.python.org/dev/peps/pep-0008/)). – Tim Pietzcker Apr 30 '14 at 07:12

1 Answers1

0

As the error says, you have an 'inconsistent use of tab and space'. In the beginning you seem to be using spaces but later you're using tabs. Be consistent. Either use spaces entirely (recommended) or use tabs entirely. This has nothing to do with IDEs or text editors. That's how python is.

Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52
  • well, not really .. my code is very small (which I paste), and nowhere I used any space in the beginning or end of a line .. always TAB, and I changed the settings for TAB = 4 space in my editor ... :-S – abdfahim Apr 30 '14 at 07:20
  • 1
    Actually, the code you have posted *does* have spaces in the beginning and uses tabs later on. – Jayanth Koushik Apr 30 '14 at 07:22
  • use tabs as default indentation – Senthilnathan Apr 30 '14 at 07:25
  • @senthilnathang: Actually PEP 8 says 'Use 4 spaces per indentation level.' – Jayanth Koushik Apr 30 '14 at 07:28
  • @JayanthKoushik yes right normally we use 4 spaces per indentation – Senthilnathan Apr 30 '14 at 07:30
  • @JayanthKoushik please do not answer duplicates, just close them as duplicate. By posting this answer, the question is no longer eligible for automatic deletion even though it has been closed as a duplicate (more precisely, this is due to OP accepting the answer; but an upvote to your answer would have the same effect). It's of course your choice what to do with this answer, but I'd advise you to delete it so that this question can be deleted by the system in a week. – l4mpi Apr 30 '14 at 07:38
  • Cannot delete accepted answer.... – Jayanth Koushik Apr 30 '14 at 07:41
  • Hmm, too bad. Well, no use in worrying about a single question, just keep it in mind for future questions :) – l4mpi Apr 30 '14 at 07:42