鱼C论坛

 找回密码
 立即注册
查看: 2098|回复: 6

关于字符串中有字典格式的怎么能转换成字典

[复制链接]
发表于 2021-10-5 21:42:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
比如这样的字符串
e=[{"id":"11","name":"北京","code":"11"},
                     {"id":"18","name":"河北","code":"18"},
                     {"id":"38","name":"福建","code":"38"},
                     {"id":"75","name":"江西","code":"75"},
                     {"id":"17","name":"山东","code":"17"},
                     {"id":"76","name":"河南","code":"76"},
                     {"id":"71","name":"湖北","code":"71"},
                     {"id":"74","name":"湖南","code":"74"},
                     {"id":"51","name":"广东","code":"51"},
                     {"id":"59","name":"广西","code":"59"},
                     {"id":"13","name":"天津","code":"13"},
                     {"id":"50","name":"海南","code":"50"},
                     {"id":"83","name":"重庆","code":"83"},
                     {"id":"81","name":"四川","code":"81"},
                     {"id":"19","name":"山西","code":"19"},
                     {"id":"85","name":"贵州","code":"85"},
                     {"id":"86","name":"云南","code":"86"},
                     {"id":"79","name":"西藏","code":"79"},
                     {"id":"84","name":"陕西","code":"84"},
                     {"id":"87","name":"甘肃","code":"87"},
                     {"id":"70","name":"青海","code":"70"},
                     {"id":"88","name":"宁夏","code":"88"},
                     {"id":"89","name":"新疆","code":"89"},
                     {"id":"10","name":"内蒙古","code":"10"},
                     {"id":"91","name":"辽宁","code":"91"},
                     {"id":"90","name":"吉林","code":"90"},
                     {"id":"97","name":"黑龙江","code":"97"},
                     {"id":"31","name":"上海","code":"31"},
                     {"id":"34","name":"江苏","code":"34"},
                     {"id":"36","name":"浙江","code":"36"},
                     {"id":"30","name":"安徽","code":"30"}];

最外层是个字符串加列表,列表里面其实是字符串,如果先用正则去掉后大约是
[('{"id":"11","name":"北京","code":"11"},\r\n\t             {"id":"18","name":"河北","code":"18"},\r\n\t       。。。。。。这样的格式,这个怎么能把里面的字典格式都拿出来保存成一个列表加字典格式呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-5 21:57:46 | 显示全部楼层
for i in eval(e):
    print(i)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-5 21:59:12 | 显示全部楼层
>>> dict1 = "{'a':'b'}"
>>> type(dict1)
<class 'str'>
>>> dict2 = eval(dict1)
>>> dict2
{'a': 'b'}
>>> type(dict2)
<class 'dict'>
>>> 
>>> dict2['a']
'b'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-6 06:23:13 | 显示全部楼层
题主的问题很有意思,学习了:
eval 貌似只能进行一重转换,并且,如果有异常的符号的话,是会报错的
给出测试代码:
str = """[('{"id":"11","name":"北京","code":"11"},\r\n\t             {"id":"18","name":"河北","code":"18"}\r\n\t')]"""
str = str.replace("\r\n\t","")
str = str.replace(" ","")
list_a = eval(str)

for items in list_a:
    items = eval(items)
    for item in items:
        print(type(item))
        print(item)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-6 07:52:39 | 显示全部楼层
class Infomation:
    def __init__(self, id, code):
        self.id = id
        self.code = code

location = dict() # 创建字典

e = [
    {"id":"11","name":"北京","code":"11"},
    {"id":"18","name":"河北","code":"18"},
    {"id":"38","name":"福建","code":"38"},
    {"id":"75","name":"江西","code":"75"},
    {"id":"17","name":"山东","code":"17"},
    {"id":"76","name":"河南","code":"76"},
    {"id":"71","name":"湖北","code":"71"},
    {"id":"74","name":"湖南","code":"74"},
    {"id":"51","name":"广东","code":"51"},
    {"id":"59","name":"广西","code":"59"},
    {"id":"13","name":"天津","code":"13"},
    {"id":"50","name":"海南","code":"50"},
    {"id":"83","name":"重庆","code":"83"},
    {"id":"81","name":"四川","code":"81"},
    {"id":"19","name":"山西","code":"19"},
    {"id":"85","name":"贵州","code":"85"},
    {"id":"86","name":"云南","code":"86"},
    {"id":"79","name":"西藏","code":"79"},
    {"id":"84","name":"陕西","code":"84"},
    {"id":"87","name":"甘肃","code":"87"},
    {"id":"70","name":"青海","code":"70"},
    {"id":"88","name":"宁夏","code":"88"},
    {"id":"89","name":"新疆","code":"89"},
    {"id":"10","name":"内蒙古","code":"10"},
    {"id":"91","name":"辽宁","code":"91"},
    {"id":"90","name":"吉林","code":"90"},
    {"id":"97","name":"黑龙江","code":"97"},
    {"id":"31","name":"上海","code":"31"},
    {"id":"34","name":"江苏","code":"34"},
    {"id":"36","name":"浙江","code":"36"},
    {"id":"30","name":"安徽","code":"30"}]

for i in e:
    location[i["name"]] = Infomation(i["id"], i["code"]) # 将资料输入字典

# 范例
x = "山东"
print(x, location[x].id, location[x].code)

y = "四川"
print(y, location[y].id, location[y].code)

z = "上海"
print(z, location[z].id, location[z].code)
山东 17 17
四川 81 81
上海 31 31
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-6 12:15:10 | 显示全部楼层
#按你描述,和我的推测,你要处理的应该是 JSON字符串,那就json模块,若进一步处理(如数据分析),那直接 pandas 构建DF 

>>> e='[{"id":"11","name":"北京","code":"11"},{"id":"18","name":"河北","code":"18"}, {"id":"38","name":"福建","code":"38"}]'
>>> import json
>>> import pandas as pd
>>> pd.DataFrame(data=json.loads(e))
   id name code
0  11   北京   11
1  18   河北   18
2  38   福建   38
>>> 
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-7 10:43:49 | 显示全部楼层

这不是一个简单的列表,最外层的列表里面全是一个一个字符串
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-13 03:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表