gpt4 book ai didi

python - 要列出的 Unicode 字符串

转载 作者:太空宇宙 更新时间:2023-11-03 18:20:12 24 4
gpt4 key购买 nike

我有一个字典列表,它是从我的 javascript 文件发送到我的 View 的。现在,当我打印列表时,其类型显示为 unicode。我想拿回我原来的 list 。我怎样才能做到这一点?

我的列表:

[{"employee_id":13,"admin":false,"approver":false,"emp_code_or_email":"second@emp.com,120","manager":false,"emp_name":"second emp","department":"IT","position":"IT","new_emp":false,"manager_and_approver":false,"role_type":"employee","s_no":1},{"employee_id":144,"admin":false,"approver":false,"emp_code_or_email":"107anku@gmail.com,1","manager":true,"emp_name":"A1 A2","department":"IT","position":"Developer","new_emp":false,"manager_and_approver":false,"role_type":"manager","s_no":2}]

我只想从中获取employee_ids。

我尝试了 eval() 、 list()、encode('utf-8')。但没有效果。

最佳答案

您拥有的是 JSON 的字符串表示形式。我可以通过查看 "admin":false 来判断,以便您获得 employee_id。您首先需要将列表中的每个 json 表示形式转换为字典。

>>> import json
>>> data_raw = '''[{"employee_id":13,"admin":false,"approver":false,"emp_code_or_email":"second@emp.com,120","manager":false,"emp_name":"second emp","department":"IT","position":"IT","new_emp":false,"manager_and_approver":false,"role_type":"employee","s_no":1},{"employee_id":144,"admin":false,"approver":false,"emp_code_or_email":"107anku@gmail.com,1","manager":true,"emp_name":"A1 A2","department":"IT","position":"Developer","new_emp":false,"manager_and_approver":false,"role_type":"manager","s_no":2}]'''
>>> data_json = json.loads(data_raw)
>>> [data['employee_id'] for data in data_json]
[13, 144]
>>>

关于python - 要列出的 Unicode 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24285325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com