J'ai un fichier json, je veux lire le fichier json en django python.

Le fichier Json contient

{
    "ATM Cash" : ["withdrawal"],
    "Expense" :["fees","goods","stationery","purchase","material","telephone"],
    "Income" : ["salary","deposit","rewards"],
    "Payment" : ["tranfer","payment"],
    "Medical" : ["dr ", 
       "doctor","dr.","nursing","pharmacist","physician","hospital","medicine"],
    "Food/Restaurent" :["food","catering"],
    "Groceries" : ["big bazar"],
    "Shopping" : ["cloths"],
    "Mobile recharge" : ["airtel"],
    "Auto & Fuel" : ["fuel"],
    "Travel" : ["travel"],
    "General" : ["others"]
}
0
rakesh 23 mai 2018 à 16:20

3 réponses

Meilleure réponse
import json 

    json_data = '{
        "ATM Cash" : ["withdrawal"],
        "Expense" :["fees","goods","stationery","purchase","material","telephone"],
        "Income" : ["salary","deposit","rewards"],
        "Payment" : ["tranfer","payment"],
        "Medical" : ["dr ", 
           "doctor","dr.","nursing","pharmacist","physician","hospital","medicine"],
        "Food/Restaurent" :["food","catering"],
        "Groceries" : ["big bazar"],
        "Shopping" : ["cloths"],
        "Mobile recharge" : ["airtel"],
        "Auto & Fuel" : ["fuel"],
        "Travel" : ["travel"],
        "General" : ["others"]
    }'


    data = json.loads(json_data)

Faites-le simplement mais suivez d'abord le didacticiel de base.

1
J Doe 23 mai 2018 à 13:31
import json
def read_file(path):
    file = open(path, "r")
    data = file.read()
    file.close()
    return data

def read_json(path):
    return json.loads(read_file(path))

def write_json(path, data):
     return write_file(path, json.dumps(data))

def write_file(path, data):
   file = open(path, "w")
   file.write(str(data))
   file.close()
   return data

Je trouve que ces fonctions, créées par MkNxGn, sont très utiles pour écrire et lire des fichiers, y compris ceux en json.

0
Mark Cartagena 12 déc. 2018 à 20:21

S'il s'agit d'un fichier, utilisez json.load() comme

import json
with open('path/to/file/file_name.json', 'r') as f:
    my_json_obj = json.load(f)
0
sajid 23 mai 2018 à 13:45