Lexan 2020-06-15
"""json字符串到Python对象""" import json json_str = ‘[{"username": "张三", "age": 18, "country": "china"}, {"username": "lisi", "age": 20, "country": "USA"}]‘ # json字符串到Python对象 persons = json.loads(json_str) print(type(persons)) # <class ‘list‘> for person in persons: print(person) # 从json文件中读取json数据 with open(‘4_1person.json‘, ‘r‘, encoding=‘utf-8‘) as fp: persons = json.load(fp) print(type(persons)) # <class ‘list‘> for person in persons: print(person)
>>> a = " a b c ". # replace主要用于字符串的替换replace. # join为字符字符串合成传入一个字符串列表,split用于字符串分割可以按规则进行分割。>>> c =