简体   繁体   中英

Python giving an “IndentationError” for no apparent reason

I have this short code snippet, which doesn't work, and keeps on giving me this error:

iMac-di-Luca:〜luca $ ./peasantlr.py |文件“ ./peasantlr.py”,第24行|用于数据中的字符:| IndentationError:unindent与任何外部缩进级别都不匹配

I don't really find anything wrong in my code... I've checked, and there's no non-valid ASCII characters which may cause this error.

#!/usr/bin/env python

def posts(data):

    postdata = ""

    for char in data:
        # If it's a non-escaped {, then it's the beginning of a post.
        if char == "{":
            insidepost = True
            # Skip to the char after {, start copying from there
            continue
        # If it's a non-escaped }, yield the post, and clean the buffer.
        if char == "}":
            insidepost = False
            yield postdata.replace("&lc;","{").replace("&rc;","}")
            postdata = ""
        # While in a post, copy the data into the post buffer.
        if insidepost:
            postdata += char

def findtags(data):
    tagdata = ""
    for char in data: *[This is the line which causes the error]*
        if char == "[":
            insidetag = True
            continue
        if char == "]":
            insidetag = False
            yield postdata
            postdata = ""
        if insidepost:
            postdata += char


f = """{A}{B}{c}{dDD}"""
for f in posts(f): print f

Does anyone know how to fix this? Many thanks in advance.

You've mixed tabs with spaces, which you shouldn't do. Per the PEP 8 Style Guide spaces are used more than tabs, so try get into using spaces :D.

Check for the presence of tabs. Eg: in vim's normal mode, type: /\\t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM