python 初学 读取文件
python 2.7 环境,读取一个 json字符串的文件,并解析。
创建一个json 文件 test.txt
{
"Id": "zh-cn",
"Name": "中国",
"Date": "2018-02-02 23:59:59",
"ArrayList": [
{
"AId": "1",
"AName": "/tmp/1广东.txt",
"Size": 1024
},
{
"AId": "2",
"AName": "/tmp/2广西.txt",
"Size": 2048
}
]
}
创建一个python 文件 readlist.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
oaslistfile=open('E:\\test','r')
linelist=oaslistfile.readlines();
linestrjoin = ''.join(str(e) for e in linelist)
print linestrjoin
load_json=json.loads(linestrjoin)
print load_json
for i in load_json["ArrayList"]:
print i["AId"],i["AName"],i["Size"]
输出如下
>python "e:\readlist.py"
{
"Id": "zh-cn",
"Name": "涓浗",
"Date": "2018-02-02 23:59:59",
"ArrayList": [
{
"AId": "1",
"AName": "/tmp/1骞夸笢.txt",
"Size": 1024
},
{
"AId": "2",
"AName": "/tmp/2骞胯タ.txt",
"Size": 2048
}
]
}
{u'Date': u'2018-02-02 23:59:59', u'ArrayList': [{u'AId': u'1', u'AName': u'/tmp/1\u5e7f\u4e1c.txt', u'Size': 1024}, {u'AId': u'2', u'AName': u'/tmp/2\u5e7f\u897f.txt', u'Size': 2048}], u'Id': u'zh-cn', u'Name': u'\u4e2d\u56fd'}
1 /tmp/1广东.txt 1024
2 /tmp/2广西.txt 2048