📅  最后修改于: 2022-03-11 15:01:34.230000             🧑  作者: Mango
import pandas as pd
import json
import numpy as np
# Create a file (csv) for test purposes
data = '''\
3,10,3.5,abc
3,010,3,bcd'''
file = io.StringIO(data)
# Create a file (json) for test purposes
json_data = '''\
{
"header":[
["field1","int16"],
["field2","float32"],
["field3","float64"],
["field4","str"]]
}'''
# Load json to dictionary
json_d = json.loads(json_data)
# Fetch field names and dtypes
names = [i[0] for i in json_d['header']]
dtype = dict(json_d['header'])
# Now use pandas to read the whole thing to a dataframe
df = pd.read_csv(file,header=None,names=names,dtype=dtype)
# Output as dict (this can be passed to a json file with json.dump())
df.to_dict('r')