White space issues with python

I am currently using Komodo Edit on Windows 7, however I ran into this problem on my Mac using TextWrangler. For some reason I am getting some kind of missed error, which is a huge problem when I write in Python. For example everything looks correct, but Komodo is currently giving me an "Ambiguous spaces" error

//How it appears in my editor
def SaveList(self, directory):
    templist = []
    templistbox2 = []
    for n,i in enumerate(self.listbox2.get(0,END)): 
       templistbox2.insert(n, re.sub(r'^[0-9]*[.]',"",str(i)))
    for filename in sorted(os.listdir(directory)):
        self.templist.insert(i, filename)
        print filename #whitespace error here

      

Considering that I've experienced this with two different editors on both windows and Mac, I'm wondering if there is some setting that I'm not aware of, or if I'm doing something wrong.

+3


source to share


3 answers


When I copy your code to a file, test.py and run

cat -A test.py

      

I see

//How it appears in my editor$
def SaveList(self, directory):$
    templist = []$
    templistbox2 = []$
    for n,i in enumerate(self.listbox2.get(0,END)): $
       templistbox2.insert(n, re.sub(r'^[0-9]*[.]',"",str(i)))$
    for filename in sorted(os.listdir(directory)):$
        self.templist.insert(i, filename)$
^I    print filename #whitespace error here$

      

which indicates that there is a tab (presented ^I

) followed by four spaces on the last line.

I'm not sure what would be the equivalent tool on Windows, but Mac must have the command cat -A

. It will show you where the bookmarks and spaces are.


There is a program called reindent.py that will convert tabs to spaces for you:



reindent.py test.py

      

Unix also has the unexpand command , which converts spaces to bookmarks.


Most Python programmers use spaces rather than tabs for indentation. Most of the Python code you find on the Internet will use spaces rather than tabs.

Your editor can add tabs, but if you grabbed the code snippet from the web, your file can now contain both tabs and spaces.

Your best bet is to go with the flow and adopt a whitespace termination convention so you don't have to reformat other people's code.

By the way, accepting the convention of spaces as indentation does not mean that you have to press SPACE4 times for each level of indentation. Your editor should have a config option that presses TABinsert 4 spaces.

+6


source


This is a common problem in Python. The following tip might help you:

1) Never mix spaces and tabs. Use spaces, not tabs, for new projects. See PEP8 . I recommend using 4 spaces.

2) Change the default values ​​for tab lengths in Komodo to make it easier to spot mixes. Click the menu Edit > Preferences

, then in Editor settings

:

  • Clear the check box Prefer Tab characters over spaces

    .
  • Use 4 for Number of spaces for indent

  • use a different value (like 8) for width of each tab character



3) the reindent.py

script in C:\Python2x\Tools\Scripts\

can help you store files correctly

-d (--dryrun)   Dry run.   Analyze, but don't make any changes to, files.
-r (--recurse)  Recurse.   Search for all .py files in subdirectories too.
-n (--nobackup) No backup. Does not make a ".bak" file before reindenting.
-v (--verbose)  Verbose.   Print informative msgs; else no output.
-h (--help)     Help.      Print this usage information and exit.

Change Python (.py) files to use 4-space indents and no hard tab characters.
Also trim excess spaces and tabs from ends of lines, and remove empty lines
at the end of files.  Also ensure the last line ends with a newline.

      

I hope this helps

+2


source


Since no one else has mentioned this, there is an easy way to make the whitespace visible right inside Komodo itself. You don't need to use external tools.

Just select View / View Whitespace from the menu, or use the keyboard shortcut Ctrl + Shift + 8 on Windows. Repeat to turn it off.

While you're at the View menu, check out some other useful features. I find the retreat guidelines especially helpful and keep them on at all times. EOL markers are also useful from time to time.

+1


source







All Articles