如何修复缩进错误?

时间:2018-11-03 16:58:00

标签: python

我遇到了缩进错误。我已经尝试使用代码编辑器并使用autopep8

将所有选项卡转换为空格

我收到以下错误:

return (imgstring)
    ^
IndentationError: unexpected indent

下面是代码:

def Barchart(id, title, data):

    bar_chart = pygal.Bar()
    bar_chart.title = title

    data_cols = data.split(':')
    for x in range(0, len(data_cols)):
        data_num = []
        data_cols_split = data_cols[x].split(',')

        for y in range(1, len(data_cols_split)):
            print(data_cols_split[y])
            data_num.append(int(data_cols_split[y]))

        bar_chart.add(str(data_cols_split[0]), data_num)

    bar_chart.render_to_png('barchart.png')


s3 = session.resource('s3')
data = open('barchart.png', 'rb')
amazonname = str(id) + '.png'
s3.Bucket(bucket).put_object(Key=amazonname, Body=data)
data.close()
with open("barchart.png", "rb") as imageFile:
  imgstring = base64.b64encode(imageFile.read())



 return (imgstring)

1 个答案:

答案 0 :(得分:1)

这应将缩进与代码内联。检查this page out以获得python缩进最佳实践。

def Barchart(id, title, data):

    bar_chart = pygal.Bar()
    bar_chart.title = title

    data_cols = data.split(':')
    for x in range(0, len(data_cols)):
        data_num = []
        data_cols_split = data_cols[x].split(',')

        for y in range(1, len(data_cols_split)):
            print(data_cols_split[y])
            data_num.append(int(data_cols_split[y]))

        bar_chart.add(str(data_cols_split[0]), data_num)

    bar_chart.render_to_png('barchart.png')


    s3 = session.resource('s3')
    data = open('barchart.png', 'rb')
    amazonname = str(id) + '.png'
    s3.Bucket(bucket).put_object(Key=amazonname, Body=data)
    data.close()
    with open("barchart.png", "rb") as imageFile:
        imgstring = base64.b64encode(imageFile.read())



    return (imgstring)