dump() vs dumps() && load() vs loads()

 json.dumps(): json.dumps() method can convert a Python object into a JSON string.

Syntax: json.dumps(dict, indent)

json.dump(): json.dump() method can be used for writing to JSON file.

Syntax: json.dump(dict, file_pointet)

json.load () vs json.loads()

Json.load(): takes a file object and returns the json object. It is used to read JSON encoded data from a

file and convert it into a Python dictionary and deserialize a file itself i.e. it accepts a file object.

Syntax: json.load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

Json.loads(): method can be used to parse a valid JSON string and convert it into a Python Dictionary. It is mainly used for deserializing native string, byte, or byte array which consists of JSON data into Python Dictionary.

Syntax: json.loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

View More...

Saving File Json, text in python

import json with open("test.json", "a") as file: dict = {"emp1": { "name": "Nishu", "designation": "programmer" "" }} data = json.dumps(dict, indent=4) file.write("% s,\n" % data) f = open("test.json", "r") print(f.read()) View More...

Upload CSV file and read using FastApi & Pandas

from fastapi import FastAPI, File, UploadFile

import shutil

import pandas as pd

import os

app = FastAPI()

@app.post("/uploadfile")

async def create_upload_file(upload_file: UploadFile = File(...)):

path = f"uploads/{upload_file.filename}"

with open(path, 'wb') as buffer:

shutil.copyfileobj(upload_file.file, buffer)

data = pd.read_csv(os.path.abspath(path)) # read

data = data.to_dict()

print(data)

return {"filename": data}

View More...

1 2 3 Next Last