python - 将 python 响应转换为 Json 响应
问题描述
我已经编写了一个 python 代码,它可以自动读取多个扩展的数据帧,并打印 DF 的前 100 行以及它的列的类型,并且可以在同一个简单函数中添加更多东西,我目前正在研究以 JSON 格式做出响应,但仍然无法这样做,因为这是我第一次使用 Json API,因为我比编程更擅长数据分析/科学 感谢您的帮助和建议
import os
import modin.pandas as pd
def sqlread(con_w_sql, sep='#'):
con, sql = special_path.split(sep)
df = pd.read_sql(sql, con)
return df.head()
readict = {
".csv": {
"read": pd.read_csv
},
".tsv": {
"read": pd.read_csv
},
".json": {
"read": pd.read_json
},
".xlsx": {
"read": pd.read_excel
},
".xml": {
"read": pd.read_xml
},
".xls": {
"read": pd.read_excel
},
".hdf": {
"read": pd.read_hdf
},
".sql": {
"read": sqlread
}
}
def read_any(file):
_, ext = os.path.splitext(file)
df = readict[ext]["read"](file)
return df.head(100), df.dtypes
file = input("enter the path to the file you want to open : ")
read_any(file)
解决方案
我尝试了以下方法,将头部和类型放入字典中,然后在字典上使用 json.dumps() 将其转换为 JSON 对象,但它给了我一个错误:
import os
import modin.pandas as pd
import json
from distributed import Client
def sqlread(con_w_sql, sep='#'):
con, sql = special_path.split(sep)
df = pd.read_sql(sql, con)
return df.head()
readict = {
".csv": {
"read": pd.read_csv
},
".tsv": {
"read": pd.read_csv
},
".json": {
"read": pd.read_json
},
".xlsx": {
"read": pd.read_excel
},
".xml": {
"read": pd.read_xml
},
".xls": {
"read": pd.read_excel
},
".hdf": {
"read": pd.read_hdf
},
".sql": {
"read": sqlread
}
}
def read_any(file):
_, ext = os.path.splitext(file)
df = readict[ext]["read"](file)
head = df.head(100)
dtype = df.dtypes
jsonresp = {
"head": head,
"dtype": dtype
}
return json.dumps(jsonresp)
file = input("enter the path to the file you want to open : ")
read_any(file)
推荐阅读
- node.js - 如何在请求上下文之外的 nodejs Express 中捕获异步错误?
- swiftui - SwiftUI - 通过嵌套引用类型传播更改通知
- java - Spring API 中的返回请求
- python - 如何将日期偏移到月初?
- php - Yii2 - GridView 外的 dropDownList 过滤器
- pandas - 熊猫,转换为日期时间后,没有正确排序
- twitter-bootstrap - 引导链接不起作用并且 id="navbar" class="navbar-collapse collapse" 不起作用
- android - 选择在 Android 中保存原始数据
- r - 在R中形成一组对
- java - Java中接口的结构约定是什么?