1

I am wondering why this is correct:

for heading in soup.find_all("td", class_="paraheading"):
    key = " ".join(heading.text.split()).rstrip(":")
    if key in columns:
        print key
        next_td = heading.find_next_sibling("td", class_="bodytext")
        value = " ".join(next_td.text.split())
        print value
    if key == "Industry Categories":
        print key
        ic_next_td = heading.find_next_sibling("td", class_="bodytext")
        for value in ic_next_td.strings:
                print value

and this is not:

for heading in soup.find_all("td", class_="paraheading"):
    key = " ".join(heading.text.split()).rstrip(":")
    if key in columns:
        print key
        next_td = heading.find_next_sibling("td", class_="bodytext")
        value = " ".join(next_td.text.split())
        print value
    if key == "Industry Categories":
        print key
        ic_next_td = heading.find_next_sibling("td", class_="bodytext")
        for value in ic_next_td.strings:
            print value

note seemingly double indentation of print value in first code block.

Wouldn't the next level of indentation down from for value in ic_next_td.strings: be one additional indentation level from this line?

Thanks

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
Fusilli Jerry
  • 745
  • 4
  • 8
  • 16

2 Answers2

11

You are mixing tabs and spaces. Don't do this.

Run python -tt yourscript.py to detect any inconsistencies, but most of all, use spaces only throughout.

Configure your editor to use spaces for indentation, and replace all existing tabs with spaces. Most code editors have a function for that.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • there's nothing inherently wrong w/ tabs, a third of all python project prefers tabs over spaces – SilentGhost Nov 19 '12 at 12:21
  • 3
    @SilentGhost: none of the projects that I have worked with use tabs. 'a third' is rather a subjective statement, for me it's 0%. :-) – Martijn Pieters Nov 19 '12 at 12:22
  • 1
    @SilentGhost interesting - could you quote a source? – Jon Clements Nov 19 '12 at 12:22
  • @SilentGhost [PEP 8](http://www.python.org/dev/peps/pep-0008/#tabs-or-spaces) says "For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do." – glglgl Nov 19 '12 at 12:22
  • @SilentGhost The [PEP8](http://www.python.org/dev/peps/pep-0008/#tabs-or-spaces) strongly recommends using spaces instead of tabs. – Bakuriu Nov 19 '12 at 12:23
  • And PEP-8 recommendations are subjective in exactly the same way – SilentGhost Nov 19 '12 at 12:24
  • LOL - thanks @MartijnPieters. So do you press the space bar 4 times everytime you want to indent? Or do you have your text editor automatically add 4 spaces when you press tab or something? I am using TextWrangler right now and thought I had set it up to auto-indent 4 spaces when I pressed tab. Will go and check the settings now. – Fusilli Jerry Nov 19 '12 at 12:25
  • @MartijnPieters: well, for me it's 100% – SilentGhost Nov 19 '12 at 12:25
  • @SilentGhost: I'll add another one: [The Google Python style guide](http://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Indentation). My point is that your claim of 1/3rd is subjective until you provide proof. My own (subjective) experience is that no serious projects use tabs. – Martijn Pieters Nov 19 '12 at 12:25
  • @FusilliJerry: I have a text editor that turns tabs into 4 space indents. – Martijn Pieters Nov 19 '12 at 12:26
  • Thanks - for anyone else using TextWrangler turning tabs to spaces found here: http://stackoverflow.com/questions/5750361/auto-convert-tab-to-4-spaces-in-textwrangler – Fusilli Jerry Nov 19 '12 at 12:27
  • @MartijnPieters: and I cannot provide a confirmation if you keep bombarding me w/ links to PEP8 as if I'm a newbie. Thanks. – SilentGhost Nov 19 '12 at 12:27
  • @JonClements: I've read it years ago on PlanetPython, I think. Sorry, I cannot provide a link. The most-unscientific data I found is from [xkcd forum](http://forums.xkcd.com/viewtopic.php?f=40&t=58867) :) – SilentGhost Nov 19 '12 at 12:41
3

Turn on showing Tab and Space character in your editor- there could be an error

Gavelock
  • 77
  • 8