cdzjf 发表于 2021-10-5 21:42:26

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

比如这样的字符串
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       。。。。。。这样的格式,这个怎么能把里面的字典格式都拿出来保存成一个列表加字典格式呢

qq1151985918 发表于 2021-10-5 21:57:46

for i in eval(e):
    print(i)

Daniel_Zhang 发表于 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'

z5560636 发表于 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)

傻眼貓咪 发表于 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] = Infomation(i["id"], i["code"]) # 将资料输入字典

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

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

z = "上海"
print(z, location.id, location.code)山东 17 17
四川 81 81
上海 31 31

阿奇_o 发表于 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
011   北京   11
118   河北   18
238   福建   38
>>>

cdzjf 发表于 2021-10-7 10:43:49

qq1151985918 发表于 2021-10-5 21:57


这不是一个简单的列表,最外层的列表里面全是一个一个字符串
页: [1]
查看完整版本: 关于字符串中有字典格式的怎么能转换成字典