1

I've worked on a python code that automates data frames reading for multiple extensions and prints the DF's first 100 lines as well as the Types of it's columns with the possibility to add more things within the same simple function, I'm currently working on making the response in a JSON format but still unable to do so since this is my first time working with Json API as I'm more data analysis/science than programming Thanks for your help & suggestions

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)    
Hamza
  • 41
  • 5
  • 1
    Can you give an example of what you would like the JSON response to look like? –  Aug 20 '21 at 16:11
  • @DarkKnight i want to return the same things thats it does right now, I.E for a dataframe that was read using this code the response should give out the 100 lines of the DF plus the Dtypes of the columns – Hamza Aug 20 '21 at 17:03

1 Answers1

0

I tried the following by putting the head and types in a dictionary and then using json.dumps() on the dict to turn it into a JSON object but it gave me an error :

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)  
Hamza
  • 41
  • 5