-3

我浏览了这段代码并删除了所有选项卡,替换为空格。我不断收到文件“xxxx”,第 14 行,除了:IndentationError: unindent does not match any external indentation level

任何人都可以看到任何其他问题吗?

### colorize_svg.py

import csv
from BeautifulSoup import BeautifulSoup

# Read in unemployment rates
unemployment = {}
min_value = 100; max_value = 0
reader = csv.reader(open('unemployment09.csv'), delimiter=",")
for row in reader:
        try:
            full_fips=row[1]+row[2]
            rate=float(row[8].strip())
            unemployment[full_fips]=rate
        except:
            pass


# Load the SVG map
svg = open('counties.svg', 'r').read()

# Load into Beautiful Soup
soup = BeautifulSoup(svg, selfClosingTags=['defs','sodipodi:namedview'])

# Find counties
paths = soup.findAll('path')

# Map colors
colors = ["#D73027", "#FC8D59", "#FEE090", "#E0F3F8", "#91BFDB", "#4575B4"]

    # County style
    path_style = 'font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1; stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel;fill:'



# Color the counties based on unemployment rate
for p in paths:

    if p['id'] not in ["State_Lines", "separator"]:
        # pass
        try:
            rate = unemployment[p['id']]
        except:
            continue

        if rate > 10:
            color_class = 5
        elif rate > 8:
            color_class = 4
        elif rate > 6:
            color_class = 3
        elif rate > 4:
            color_class = 2
        elif rate > 2:
            color_class = 1
        else:
            color_class = 0


        color = colors[color_class]
        p['style'] = path_style + color

print soup.prettify()
4

4 回答 4

2

两件事情:

您的第一个 for 循环似乎在正文上有一个额外的标识级别,但这可能是复制/粘贴的问题。

和:

# Find counties
paths = soup.findAll('path')

# Map colors
colors = ["#D73027", "#FC8D59", "#FEE090", "#E0F3F8", "#91BFDB", "#4575B4"]

    # County style
    path_style = 'font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1; stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel;fill:'

正如您在此处看到的,path_style 应该与颜色对齐。那可能就是您的错误所在。

于 2012-08-01T14:18:43.410 回答
1

When I paste your code into Python, it only brings up one IndentationError - at:

    # County style
    path_style = 'font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1; stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel;fill:'

Which should not be indented, and so gives a slightly different error to the one you're seeing ("unexpected indent").

This means that the IndentationError you are seeing must be caused by a mix of tabs and spaces, which won't be maintained when copying and pasting between your code and StackOverflow. Line 14 is the except: in the top for loop - you will find that it and the try associated with it have a different combination of tabs and spaces.

The are various fixes to this - for example, you could remove the indentation from the front of the try: and except: both and replace with uniform indentation (hit tab once for both of them, having made sure you have tabs-to-spaces set or unset to match the style used in the rest of the file). But the easier way would be to just copy and paste the code from your question back into the .py file.

于 2012-08-01T14:38:41.647 回答
1

您的缩进错误很有可能是因为没有用空格替换每个制表符。这肯定经常发生在我身上 - 只需再次检查并确认没有标签。

于 2012-08-01T14:31:19.547 回答
0

在允许您查看隐藏空格和选项卡的编辑器中检查您的代码。混合制表符和空格给我带来了很多这样的问题——只坚持一种缩进形式。

于 2012-08-01T14:33:24.833 回答