简体   繁体   中英

How to parse intagram data json with python 2.7?

I've got a json file of instagram followers that I'm trying to import into python to manipulate further. I've tried several methods to get all followers into a list or export to CSV but can't seem to iterate through the json properly.

This is the json structure:

{
    close_friends:  {
        user1: 2019-02-12T04:57:38
    },
    follow_requests sent:   {
        user2: 2019-02-01T11:27:37
    },
    followers:  {
        user3:2019-02-12T02:14:40,
        userN:YYYY-MM-DDTXX:XX:XX
    }
}

I've tried a few different solutions I've found here, tweaked for my context, but keep getting different errors.

import json
parsed_json = json.loads("test.json")
print parsed_json["followers"]

This gives me a "ValueError: No JSON object could be decoded" error. I'm reading into json documentation but can't figure out if my json is not formatted properly (missing quotes, or not flattened).

My goal is to have a list with all follower names (so user3 to userN). Is there any simple way to achieve this with python?

Thank you.

1) json.loads takes an actual JSON string, not a file name.

You would need json.load(f) , where f is a file-handle.

2) Your keys and string-values (like dates) must all be surrounded by quotes, as per the JSON specification to be valid


FWIW, any new Python development should be using Python3, as Python2 will be deprecated soon (2020).

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