"expected an indented block" which I couldn't figure out

Here's my script in python and I got error like this:

File "overlap.py", line 5
    for line in infile:
      ^
IndentationError: expected an indented block

I couldn't figure out the error, the input is json file download from yelp. The script was coded in vim, I double checked the indentation again and again and it seems nothing wrong here. Here the code:

import json

def cal_overlap(filename = "yelp_academic_dataset_review.json"):
    with open(filename, 'rb') as infile:
        for line in infile:
            data = json.loads(line)
            buz = data["business_id"]
            user_id = data["user_id"]
            if user_id in result:
                if buz not in result[user_id]:
                    result[user_id].append(buz)
            else:
                result[user_id] = []
                result[user_id].append(buz)
        return result

def plot(res):
    s = 0
    count = 0
    x = []
    y = []
    for key in res:
        count += 1
        s += len(res[key])
    return float(s) / count

def main():
    res = cal_overlap()
    print plot(res)


if __name__ == '__main__':
    main()

Upvotes: 0

Views: 961

Answers (3)

khelwood
khelwood

Reputation: 59111

It looks like you are mixing tabs and spaces in your indentation. That might account for the indentation looking right to you but not to the interpreter.

Stick to using all spaces for your indentation.

Edit:

If you are using a shebang in your python scripts, you can specify the argument -tt to check if you are using a mixture of tabs and spaces in your indentation.

E.g.

#!/usr/bin/env python -tt

Upvotes: 2

southbayjack
southbayjack

Reputation: 3

Perhaps you have a mix of tabs and spaces in your code? Just use all spaces for your indentation otherwise you'll get some strange errors.

Upvotes: 0

nneonneo
nneonneo

Reputation: 179452

I don't see a problem at for line in infile:, but your second-last line is wrong:

if __name__ = '__main__':

needs to read

if __name__ == '__main__':

I fixed that and the module compiles fine.

Upvotes: 0

Related Questions