微信小程序的云数据库的数据是可以直接导出的,支持导出成JSON文件或者CSV文件。
json文件处理起来很方便,这里就直接导出成json文件了。
导出结果举例(data.json):
1 2 3 4 {"_id" :"058dfefe629caed7075a6b8725c45ac1" ,"_openid" :"oaaaaaaaaL-7777777777" ,"isbn" :"9787530677216" } {"_id" :"b69f67c0629caee605fe79f43980a838" ,"_openid" :"oaaaaaaaaL-7777777777" ,"isbn" :"9787108064554" } {"_id" :"8f75309d629caf0c072a3f7a1f20df26" ,"_openid" :"oaaaaaaaaL-7777777777" ,"isbn" :"4781114262343" } {"_id" :"16db756f629caf3905ef16d24f5638f3" ,"isbn" :"9787508669762" ,"_openid" :"oaaaaaaaaL-7777777777" }
虽然导出来是.json文件,但这其实也不是标准的json格式的文件。如果是标准格式,应该是这样的:
1 2 3 4 [{"_id" :"058dfefe629caed7075a6b8725c45ac1" ,"_openid" :"oaaaaaaaaL-7777777777" ,"isbn" :"9787530677216" }, {"_id" :"b69f67c0629caee605fe79f43980a838" ,"_openid" :"oaaaaaaaaL-7777777777" ,"isbn" :"9787108064554" }, {"_id" :"8f75309d629caf0c072a3f7a1f20df26" ,"_openid" :"oaaaaaaaaL-7777777777" ,"isbn" :"4781114262343" }, {"_id" :"16db756f629caf3905ef16d24f5638f3" ,"isbn" :"9787508669762" ,"_openid" :"oaaaaaaaaL-7777777777" }]
如果是标准的json格式的文件,可以用json.load(fp)
函数直接读取文件内容并转换为python的类型。
因为并不是标准的json格式的文件,所以就当成普通的文本文件就行。分两步:
1.直接按普通文本文件读取内容;
2.再对读取的数据进行格式处理。
1.读取json文件内容(非json标准格式) 1 2 3 4 5 6 7 8 9 10 def read_non_standard_json (json_file ): data = [] with open (json_file, 'r' , encoding='utf-8' ) as f: for line in f: data.append(line.strip('\n' )) f.close() return data
返回的data是个List,每一行是一个字符串:
1 ['{"_id":"058dfefe629caed7075a6b8725c45ac1","_openid":"oaaaaaaaaL-7777777777","isbn":"9787530677216"}', '{"_id":"b69f67c0629caee605fe79f43980a838","_openid":"oaaaaaaaaL-7777777777","isbn":"9787108064554"}', '{"_id":"8f75309d629caf0c072a3f7a1f20df26","_openid":"oaaaaaaaaL-7777777777","isbn":"4781114262343"}', '{"_id":"16db756f629caf3905ef16d24f5638f3","isbn":"9787508669762","_openid":"oaaaaaaaaL-7777777777"}']
2.数据格式化处理
把data中每一项(json的字符串)反序列化为Python数据对象(dict字典)。
然后把需要的项组成一个List。
1 2 3 4 5 6 7 8 9 10 11 def data_format (data ): data_list = [] for s in data: one = json.loads(s, strict=False ) isbn = one['isbn' ] openid = one['_openid' ] data_list.append([openid, isbn]) return data_list
3.完整代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import jsondef read_non_standard_json (json_file ): data = [] with open (json_file, 'r' , encoding='utf-8' ) as f: for line in f: data.append(line.strip('\n' )) f.close() return data def data_format (data ): data_list = [] for s in data: one = json.loads(s, strict=False ) isbn = one['isbn' ] openid = one['_openid' ] data_list.append([openid, isbn]) return data_list print ('-' *10 + '读取json文件内容(非json标准格式)' + '-' *10 )json_file = r'.\data.json' data = read_non_standard_json(json_file) print (data)print ('-' *10 + '格式化' + '-' *10 )data_list = data_format(data) print (data_list)
运行结果:
1 2 3 4 ----------读取json文件内容(非json标准格式)---------- ['{"_id":"058dfefe629caed7075a6b8725c45ac1","_openid":"oaaaaaaaaL-7777777777","isbn":"9787530677216"}', '{"_id":"b69f67c0629caee605fe79f43980a838","_openid":"oaaaaaaaaL-7777777777","isbn":"9787108064554"}', '{"_id":"8f75309d629caf0c072a3f7a1f20df26","_openid":"oaaaaaaaaL-7777777777","isbn":"4781114262343"}', '{"_id":"16db756f629caf3905ef16d24f5638f3","isbn":"9787508669762","_openid":"oaaaaaaaaL-7777777777"}'] ----------格式化---------- [['oaaaaaaaaL-7777777777', '9787530677216'], ['oaaaaaaaaL-7777777777', '9787108064554'], ['oaaaaaaaaL-7777777777', '4781114262343'], ['oaaaaaaaaL-7777777777', '9787508669762']]