“inconsistent use of tabs and spaces in indentation”

后端 未结 28 2233
北恋
北恋 2020-11-22 00:56

I\'m trying to create an application in Python 3.2 and I use tabs all the time for indentation, but even the editor changes some of them into spaces and then print out \"inc

相关标签:
28条回答
  • 2020-11-22 01:22

    With the IDLE editor you can use this:

    • Menu EditSelect All
    • Menu FormatUntabify Region
    • Assuming your editor has replaced 8 spaces with a tab, enter 8 into the input box.
    • Hit select, and it fixes the entire document.
    0 讨论(0)
  • 2020-11-22 01:23

    There was a duplicate of this question from here but I thought I would offer a view to do with modern editors and the vast array of features they offer. With python code, anything that needs to be intented in a .py file, needs to either all be intented using the tab key, or by spaces. Convention is to use four spaces for an indentation. Most editors have the ability to visually show on the editor whether the code is being indented with spaces or tabs, which helps greatly for debugging. For example, with atom, going to preferences and then editor you can see the following two options:

    Then if your code is using spaces, you will see small dots where your code is indented:

    And if it is indented using tabs, you will see something like this:

    Now if you noticed, you can see that when using tabs, there are more errors/warnings on the left, this is because of something called pep8 pep8 documentation, which is basically a uniform style guide for python, so that all developers mostly code to the same standard and appearance, which helps when trying to understand other peoples code, it is in pep8 which favors the use of spaces to indent rather than tabs. And we can see the editor showing that there is a warning relating to pep8 warning code W191,

    I hope all the above helps you understand the nature of the problem you are having and how to prevent it in the future.

    0 讨论(0)
  • 2020-11-22 01:23

    Solving this using Vim editor

    1. Open terminal (Ctrl + Alt + T).
    2. Go to the directory where the file is located (cd <path_to_your_directory>). Ex: cd /home/vineeshvs/work.
    3. Open the file in Vim (vim <file_name>). Ex: vim myfile.txt .
    4. [Optional step] Enable search keyword highlighting in Vim (ESC :set hlsearch)
    5. Go to the line where you have this problem (ESC :<line_number>). Ex: :53 in Vim editor after pressing ESC button once.
    6. Replace tabs using the required number of spaces in Vim (:.,$s/\t/<give_as_many_spaces_as_you_want_to_replace_tab>/gc). Ex: Tab will be replaced with four spaces using the following command: :.,$s/\t/ /gc after pressing ESC button once). This process is interactive. You may give y to replace the tab with spaces and n to skip a particular replacement. Press ESC when you are done with the required replacements.
    0 讨论(0)
  • 2020-11-22 01:24

    I had the same problem and fix it using following python script. hope it help others.

    it is because of using tabs and spaces for indenting code. in this script I replace each tab with four spaces.

    input_file = "source code path here"  # e.g. source.py
    output_file = "out put file path here"  # e.g out.py
    with open(input_file, 'r') as source:
        with open(output_file, 'a+') as result:
            for line in source:
                line = line.replace('\t', '    ')
                result.write(line)
    

    if you use sublime or any other editor which gives you the tool to replace text you can replace all tabs by four spaces from editor.

    0 讨论(0)
  • 2020-11-22 01:27

    For VSCode users

    Ctrl+Shift+P or View->Command Palette.

    Type

    >Convert Indentation to Spaces

    press Enter.

    0 讨论(0)
  • 2020-11-22 01:28

    What I did when the same error popped up: Select everything (Str + A) and press Shift + Tab. So nothing was indented anymore. Now go back to the lines you want to have indented, and put it back how you want it.

    It worked for me...

    0 讨论(0)
提交回复
热议问题