K
K
Kot Kotov2020-05-16 11:13:31
Python
Kot Kotov, 2020-05-16 11:13:31

How to extract certain objects from a string in python?

I work with vk_api, with such a request
usr = vk.users.get(user_id=event.user_id)
I get this:

[{'id': мой ади, 'first_name': 'имя', 'last_name': 'фамилия', 'is_closed': False, 'can_access_closed': True}]

How can I get only the last name and first name from all this?
I will be very grateful

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-05-16
@kot123123

Another bot-writing who does not want to learn the basics of the language, they are tired.
PS: go learn the basics, work with data structures - lists and dictionaries.

K
Konstantin Malyarov, 2020-05-16
@Konstantin18ko

Kot Kotov don't listen to them.
There is such a JSON format, this is just your string.
To do so: usr[0]["last_name"] will be only the most uneducated person.
So let's deal with your JSON:

user = vk.users.get(user_id=event.user_id)
user_json = json.loads(user)
print(user_json['last_name'])

In this case, if the order in the array changes, the value will always be found.
If you need help, write in a telegram @nanoCBAPKA

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question