python config file json datatypes

Solutions on MaxInterview for python config file json datatypes by the best coders in the world

showing results for - "python config file json datatypes"
Tristan
06 Nov 2019
1import pandas as pd
2import json
3import numpy as np
4
5# Create a file (csv) for test purposes 
6data = '''\
73,10,3.5,abc
83,010,3,bcd'''
9file = io.StringIO(data)
10
11# Create a file (json) for test purposes
12json_data = '''\
13{
14   "header":[
15       ["field1","int16"],
16       ["field2","float32"],
17       ["field3","float64"],
18       ["field4","str"]]
19}'''
20
21# Load json to dictionary
22json_d = json.loads(json_data)
23
24# Fetch field names and dtypes
25names = [i[0] for i in json_d['header']]
26dtype = dict(json_d['header'])
27
28# Now use pandas to read the whole thing to a dataframe
29df = pd.read_csv(file,header=None,names=names,dtype=dtype)
30
31# Output as dict (this can be passed to a json file with json.dump())
32df.to_dict('r')
33