首页 > 解决方案 > 将编辑后的 ​​CSV 输入写入 JSON 文件

问题描述

我正在读取一个 CSV 文件并输出一个 JSON 文件,然后由另一个应用程序处理该文件。下游应用程序所需的某些信息不存在于 CSV 文件中,需要以编程方式从其他来源获取。在输出 JSON 数据之前,有没有办法在 CSV 数据中编辑/插入字段?我一直在研究Python CSV 模块文档并注意到了这个for row in reader:例子。我可以遍历已知字段,然后插入新字段以构建new_row变量。我可以转储new_row到 JSON 文件,但给原始文件的字段名称reader不会随之而来,所以我最终得到了一个非常疯狂的new_row变量,感觉不像我在保存任何东西。想法?

#!/usr/bin/python

import urllib
import json
import sys
import csv

fieldnames = ("first_name", "last_name","phone")
reader = csv.DictReader(csvfile, fieldnames)
all_lines = list(reader)

jsonfile.write ('{"people":[')

for row in all_lines:
   jsonfile.write('{"person":')

   #address = my_get_address (...)
   new_row = ["first_name", row['first_name'], "last_name", row['last_name'], "phone", row['phone'], 'address', '123 anystreet']
   json.dump(new_row, jsonfile)

   jsonfile.write('}')
   # comma + newline for every person excep the last
   if row != all_lines[-1]:
      jsonfile.write (',\n')

jsonfile.write (']}')

JSON输出:

{                                                                                                                                                                                       
   "people" : [                                                                                                                                                                         
      {                                                                                                                                                                                 
         "person" : [                                                                                                                                                                  
            "first_name",                                                                                                                                                              
            "joe",                                                                                                                                                                     
            "last_name",                                                                                                                                                               
            "smith",                                                                                                                                                                   
            "phone",                                                                                                                                                                   
            "999-555-1212",                                                                                                                                                            
            "address",                                                                                                                                                                 
            "123 anystreet"                                                                                                                                                            
         ]                                                                                                                                                                             
      },                                                                                                                                                                               
      {                                                                                                                                                                                
         "person" : [                                                                                                                                                                  
            "first_name",                                                                                                                                                              
            "mary",                                                                                                                                                                    
            "last_name",                                                                                                                                                               
            "jones",                                                                                                                                                                   
            "phone",                                                                                                                                                                   
            "888-555-1212",                                                                                                                                                            
            "address",                                                                                                                                                                 
            "123 anystreet"
         ]
      }
   ]
}

标签: pythonjsoncsv

解决方案


我假设您的 .csv 文件格式如下:

first_name,last_name,phone
abc,abc,123
def,def,456
ghi,ghi,789

然后你可能想要这样做(使用你的地址示例):

import csv 
import json 

def csv_to_json(csvFilePath, jsonFilePath):
    jsonArray = []
      
    # Read csv file
    with open(csvFilePath, encoding='utf-8') as csvf: 
        # Load csv file data using csv library's dictionary reader
        csvReader = csv.DictReader(csvf) 

        # Convert each csv row into python dict
        for row in csvReader: 
            # Update row with new fields
            row.update({'address': '123 anystreet'})
            # Add this python dict to json array
            jsonArray.append(row)
  
    # Convert python jsonArray to JSON String and write to file
    with open(jsonFilePath, 'w', encoding='utf-8') as jsonf: 
        jsonString = json.dumps(jsonArray, indent=2)
        jsonf.write(jsonString)
          
csvFilePath = r'test.csv'
jsonFilePath = r'test.json'
csv_to_json(csvFilePath, jsonFilePath)

来源

生成的 .json 文件:

[
  {
    "first_name": "abc",
    "last_name": "abc",
    "phone": "123",
    "address": "my_address"
  },
  {
    "first_name": "def",
    "last_name": "def",
    "phone": "456",
    "address": "my_address"
  },
  {
    "first_name": "ghi",
    "last_name": "ghi",
    "phone": "789",
    "address": "my_address"
  }
]

编辑

要像您的问题一样格式化输出,您可以将这两行更改为换行rowjsonArray这样:

jsonArray.append({'person': row})
  
jsonString = json.dumps({'people': jsonArray}, indent=2)

现在它给出:

{
  "people": [
    {
      "person": {
        "first_name": "abc",
        "last_name": "abc",
        "phone": "123",
        "address": "my_address"
      }
    },
    {
      "person": {
        "first_name": "def",
        "last_name": "def",
        "phone": "456",
        "address": "my_address"
      }
    },
    {
      "person": {
        "first_name": "ghi",
        "last_name": "ghi",
        "phone": "789",
        "address": "my_address"
      }
    }
  ]
}

推荐阅读