0

I'm new in using python. I've started with some json data, pandas and numpy.

I have a list of json objects stored into a txt file. I'm reading the data from the txt file and create a dictionary and then append to my json list each json object from the file.

jsonTweetsList = []
with open('tweetsOutput.txt') as f:
    for jsonObj in f:
        tweetDict = json.loads(jsonObj)
        jsonTweetsList.append(tweetDict)

An example of what the file contains:

{"created_at":"Sat May 09 12:25:06 +0000 2020","text":"#wbdemotest disliked the taxes and fees","user": {"id":97750949378440806,"id_str":"977509493784408064","name":"Roxana Cepoiu","screen_name":"roxana_cepoiu"}}

{"created_at":"Sat May 09 12:25:44 +0000 2020","id":1259097218260312065,"id_str":"1259097218260312065","text":"#wbdemotest happy with new services provided","user":{"id":977509493784408064,"id_str":"977509493784408064","name":"Roxana Cepoiu","screen_name":"roxana_cepoiu"}}

I try to access the name element from the user, but I do not know how. Using the below script it does not work for the user-name element, it is working only for created_at and text.

for tweet in jsonTweetsList:
    print(tweet["created_at"], tweet["text"],tweet["user"][0]["name"])

Can you advise me on how can I access the data from user-name?

1
  • 1
    Try tweet['user']['name']. Commented May 13, 2020 at 19:03

1 Answer 1

1

user is not a list, it's just an object, what you need to do is access it directly without the [0] index

print(tweet["user"]["name"])
Sign up to request clarification or add additional context in comments.

1 Comment

Worked in this way. Haha, thank you! Hard times being a beginner :D

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.