Tuesday, November 5, 2019

Python - function to convert .csv file to multiple .json files

This function was used in QA process, when I have one input of .csv file, and I want to test each individual row of data
.
This is production.csv file:
api,prod_date,oil,water,cond,gas
42003024530000,2014-04-01,22,34,0,4928
42003024530000,2014-05-01,30,334,0,4328
42003024530000,2014-06-01,20,44,0,2228
42003024530000,2014-07-01,20,164,0,3328
42003024530000,2014-08-01,0,164,46,3600

This is how data looks like when written into individual json file:
[{
    "api": "42003024530000",
    "prod_date": "2014-04-01",
    "oil": "22",
    "water": "34",
    "cond": "0",
    "gas": "4928"
}]

# Convert csv data into jsondef convert_write_json(data, json_file_name, directory):
    with open(os.path.join(current_directory, 'TestData', directory, json_file_name + '.json'), "w") as f:
        f.write('[' + json.dumps(data, sort_keys=False, indent=4, separators=(',', ': ')) + ']')


Method 1: right file into a single json file
# Read CSV File and write into a single json filedef read_production_csv(csv_file_name, json_file, directory):
    for item in directory:
        csv_rows = []
        csv_file_path = os.path.join(current_directory, 'TestData', csv_file_name + '.csv')
        with open(csv_file_path) as csv_file:
            reader = csv.DictReader(csv_file)
            field = reader.fieldnames
            for row in reader:
                csv_rows.extend([{field[i]:row[field[i]] for i in range(len(field))}])
            convert_write_json(csv_rows, json_file, item)

Method 2: right each row in the file into a separate json file
# Read CSV File and write each row into a separate json file
def read_production_csv(csv_file_name, directory):
    for item in directory:
        csv_file_path = os.path.join(current_directory, 'TestData', csv_file_name + '.csv')
        with open(csv_file_path) as csv_file:
            reader = csv.DictReader(csv_file)
            for row in reader:
                api = row['api']
                prod_date = row['prod_date']
                json_file_name = 'production_' + api + '_' + prod_date
                convert_write_json(row, json_file_name, item)