1

我已经编写了一个 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)    
4

1 回答 1

0

我尝试了以下方法,将头部和类型放入字典中,然后在字典上使用 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)  
于 2021-08-21T12:49:17.520 回答