json to dict
来源:互联网 发布:js获取传过来的参数 编辑:程序博客网 时间:2024/05/24 06:48
py2 window10
#!/usr/bin/env python# -*- coding:utf-8 -*- # Author: json_steveimport jsonimport csvimport sysreload(sys)sys.setdefaultencoding('utf-8')def json_to_csv(): # json文件 json_file = open('.json', 'r') # csv文件 csv_file = open('.csv', 'w') # csv读写器 csv_writer = csv.writer(csv_file) # 表头和正文 data = json.load(json_file) # 表头 sheet_title = data[0].keys() # 内容 content_list = [] for dic in data: content_list.append(dic.values()) # content_list = [dic.values() for dic in data] # 5.通过读写器 写入csv文件 csv_writer.writerow(sheet_title) csv_writer.writerows(content_list) # 6.关闭文件 json_file.close() csv_file.close()if __name__ == '__main__': json_to_csv()
py3 window10
#!/usr/bin/env python# -*- coding:utf-8 -*- # Author: json_steveimport jsonimport csvdef json_to_csv(): # json文件 json_file = open('.json', 'r',encoding='utf-8') # csv文件 csv_file = open('.csv', 'w',encoding='utf-8') # csv读写器 csv_writer = csv.writer(csv_file) # 表头和正文 data = json.load(json_file) # 表头 sheet_title = data[0].keys() # 内容 content_list = [] for dic in data: content_list.append(dic.values()) # 5.通过读写器 写入csv文件 csv_writer.writerow(sheet_title) csv_writer.writerows(content_list) # 6.关闭文件 json_file.close() csv_file.close()if __name__ == '__main__': json_to_csv()
注:py2 和 py3的不同:1,对字典操作的values()函数稍有不同,2,默认编码不同。py2
>>> d = {1:1,2:2,3:3}>>> d.values()[1, 2, 3]>>> import sys>>> sys.getdefaultencoding()'ascii'
py3
>>> d = {1:1,2:2,3:3}>>> d.values()dict_values([1, 2, 3])>>> import sys>>> sys.getdefaultencoding()'utf-8'
阅读全文
0 0
- dict to json And json to dict
- json to dict
- python json转换异常:“In order to allow non-dict objects to be serialized..."
- json dict python base64
- python dict to dataframe
- mongodb取出json,利用python转成dataframe(dict-to-dataframe)
- python 中Dict 转 Json
- json.dump() and sorted() dict
- python dict与json转换
- python json 转 dict , list
- python dict json读写文件
- python中将json字符串转换为dict
- xml,dict,json,vector乾坤大挪移
- python中json到dict转换
- python string转为dict --- json.loads
- python中list,str,json,dict使用
- python dict转json并保存文件
- Convert stardict dict to txt file
- 辩证思维解答转载内容与seo的关系
- git push
- 一个Exception catch不住的"异常"
- 如何避免与公司名称混淆
- 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()
- json to dict
- Spring mvc通过Spring security进行密码验证加盐
- Tomcat配置任意目录下的web应用
- TabLayout +ViewPager+Fragment + RecycleView + webView加载本地html
- JavaScript For 循环
- anagular.JS Filter自定义过滤器
- 购物车到订单的附加方法
- 如何自定义安装office 2016(通过configuration.xml文件来指定安装内容和路径)
- xml 做图