0

I am trying to iterate over JSON and write to a file as the information is retrieved. My code looks like this

for i in range(100):
    identifier = data ['items'][i]['id']
    out_file.write(identifier)

When I run it I get an indent error on the out_file.write line. I have seen so much code formatted in this way and I'm baffled as to why it isn't working for me. If I make the out_file line even with the for line, the program works, but only writes the last identifier in the loop. Any advice?

1 Answer 1

4

An indent error means that the indents don't line up. You should make sure that the line out_file.write(identifier) lines up with identifier = data ['items'][i]['id'], and that you don't mix up tabs with spaces.

Sign up to request clarification or add additional context in comments.

2 Comments

... and if you demoted out_file.write all the way to left margin, it would fire after the loop had completed and only write one value - exactly what you're seeing.
Thank you. You are all quite right. The issue in my particular case was that I had some trailing tabs that were screwing things up. I think the lesson learned here is that when there are mysterious indentation errors to show the invisible characters to make sure everything is what you think it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.