将str文本类型转换为dict

---for test---

  1. 往文件写如内容:
     1 f = open("mall.txt",'w+',encoding='utf-8')
     2 goods = {
     3     '电子产品':[
     4         ['mac',7500,7],
     5         ['iphone7',5400,5],
     6         ['thunder',7600,3]
     7     ],
     8     '母婴用品':[
     9         ['帮宝适',45.9,50],
    10         ['美素佳儿',380,10]
    11     ],
    12     '摩托汽车':[
    13         ['特斯拉',820000,2],
    14         ['雷克萨斯Rx450',450000,5]
    15     ],
    16     '日常用品':[
    17         ['东北大米',38.5,20],
    18         ['上海青',3.6,47]
    19     ]
    20 }
    21 s = str(goods)
    22 f.write(s)
    23 f.close()
    View Code

     

  2. 从文件中读取内容:
    1 with open("mall.txt",'r',encoding="utf-8") as f:
    2     k = f.read()
    3     f.close()
    4 print(k)
    View Code

     

  3. 如何将读取到的type为str的类dict内容转换回dict类型呢!

    一、用eval函数:

1 s = eval(k)
2 
3 print(type(s))
4 for i in s:
5     print(i,s[i])
View Code

    二、用json函数:(但是必须注意的是,json格式要求的是双引号,单引号的话会报错【选中单引号 --> ctrl + shift + alt + j  --> 替换单引号为双引号】)

      

1 print(json.loads(goods))
View Code

      如果是单引号报错内容:

1  print(json.loads(k))
2   File "F:\Python35\lib\json\__init__.py", line 319, in loads
3     return _default_decoder.decode(s)
4   File "F:\Python35\lib\json\decoder.py", line 339, in decode
5     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
6   File "F:\Python35\lib\json\decoder.py", line 355, in raw_decode
7     obj, end = self.scan_once(s, idx)
8 json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
View Code

 

---恢复内容结束---

for test :

  1. 往文件写如内容:
     1 f = open("mall.txt",'w+',encoding='utf-8')
     2 goods = {
     3     '电子产品':[
     4         ['mac',7500,7],
     5         ['iphone7',5400,5],
     6         ['thunder',7600,3]
     7     ],
     8     '母婴用品':[
     9         ['帮宝适',45.9,50],
    10         ['美素佳儿',380,10]
    11     ],
    12     '摩托汽车':[
    13         ['特斯拉',820000,2],
    14         ['雷克萨斯Rx450',450000,5]
    15     ],
    16     '日常用品':[
    17         ['东北大米',38.5,20],
    18         ['上海青',3.6,47]
    19     ]
    20 }
    21 s = str(goods)
    22 f.write(s)
    23 f.close()
    View Code

     

  2. 从文件中读取内容:
    1 with open("mall.txt",'r',encoding="utf-8") as f:
    2     k = f.read()
    3     f.close()
    4 print(k)
    View Code

     

  3. 如何将读取到的type为str的类dict内容转换回dict类型呢!

    一、用eval函数:

1 s = eval(k)
2 
3 print(type(s))
4 for i in s:
5     print(i,s[i])
View Code

    二、用json函数:(但是必须注意的是,json格式要求的是双引号,单引号的话会报错【选中单引号 --> ctrl + shift + alt + j  --> 替换单引号为双引号】)

      

1 print(json.loads(goods))
View Code

      如果是单引号报错内容:

1  print(json.loads(k))
2   File "F:\Python35\lib\json\__init__.py", line 319, in loads
3     return _default_decoder.decode(s)
4   File "F:\Python35\lib\json\decoder.py", line 339, in decode
5     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
6   File "F:\Python35\lib\json\decoder.py", line 355, in raw_decode
7     obj, end = self.scan_once(s, idx)
8 json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
View Code

 


而字典转成列表就仅仅一个items方法就可以:

> goods = {"apple":2,"ipad pro":18}
> goods.items()
dict_items([('ipad pro', 18), ('apple', 18)])

 

转载于:https://www.cnblogs.com/mologa-jie/p/6900524.html

你可能感兴趣的:(将str文本类型转换为dict)