: {""product"": ""九次方大数据""}, ""11"": {""product"": ""疯贴-Fun""}, ""12"": {""product"": ""辣妈汇""}, ""13"": {""product"": ""四海华辰科技""}, ""14"": {""product"": ""诺尔康""}, ""15"": {""product"": ""天津象形科技""}, ""16"": {""product"": ""淘淘搜""}, ""17"": {""product"": ""梦芭莎""}, ""18"": {""product"": ""魔方公寓""}, ""19"": {""product"": ""多利农庄""}, ""20"": {""product"": ""米奇网""}, ""21"": {""product"": ""乐淘网""}, ""22"": {""product"": ""欧瑞思丹""}}"这种文件在csv 里面我用%s/""/"/g替换(把单个双引号替换2个双引号的),字符串之间的逗号为啥也没有了?这是为啥呢、### 题目描述
1 回答
陪伴而非守候
TA贡献1757条经验 获得超8个赞
用你的办法在 vim 里试着替换了一下, 正常替换, 没有重现你说的情况, 猜测和你的vim配置相关?
可参考一下下面的例子
给定一定格式的数据,因为json格式可能远比csv复杂,所以只能支持一层扁平结构的json数据:
employee_data = '{"employee_details":[{"employee_name": "James", "email": "james@gmail.com", "job_profile": "Sr. Developer"},{"employee_name": "Smith", "email": "Smith@gmail.com", "job_profile": "Project Lead"}]}'
import json import csv employee_parsed = json.loads(employee_data) emp_data = employee_parsed['employee_details'] # open a file for writing employ_data = open('/tmp/EmployData.csv', 'w') # create the csv writer object csvwriter = csv.writer(employ_data)count = 0for emp in emp_data: if count == 0: header = emp.keys() csvwriter.writerow(header) count += 1 csvwriter.writerow(emp.values()) employ_data.close()
会输出结果,key会形成表头,value会是逗号分隔的值
$ cat /tmp/EmployData.csv employee_name,email,job_profile James,james@gmail.com,Sr. Developer Smith,Smith@gmail.com,Project Lead
添加回答
举报
0/150
提交
取消