QuestionGuy
QuestionGuy

Reputation: 3

Parsing Multiple .json files

I am trying to parse multiple files dealing with "Mike's Pies" as you can see in the code below. I have written it to where I get the desired output, now I would like to parse all the files named "Mike's Pies"

 import json
 import sys
 import glob

with open("Mike's Pies.20130201.json") as json_data:
    data = json.load(json_data)
#Keep all orders with variable of r
    for r in data ["orders"]:

        orderName = r["orderPlacer"]["name"]
        #Print with address to acquire the housenumber/street/city/state
        address = r["address"]["houseNumber"]
        street = r["address"]["street"]
        city = r["address"]["city"]
        state = r["address"]["state"]
        Mikes = "Mike's Pies,"
        output = str(orderName) + ", " + str(address) + " " + str(street) +       
         " " + str(city) + " " + str(state) + ", " + Mikes + " " 



        length = len(r["pizzas"])
        for i in range (0,length):
        #if length >= 1 print r["pizzas"][1]["name"]
            #if i!=length:
            pizza = ((r["pizzas"][i]["name"].strip("\n"))).strip(" ")
            if(i!=length-1):
                output += pizza + ", "
            else:
                output += pizza

        print(output+"\n")

Upvotes: 0

Views: 1991

Answers (1)

Terry
Terry

Reputation: 36

It sounds like you have code which works on "Mike's Pies.20130201.json", and you want to run that code on every file that starts with "Mike's Pies" and ends with "json", regardless of the timestamp-like bit in the middle. Am I right? You can get all matching filenames with glob and parse them one after the other.

for filename in glob.glob("Mike's Pies.*.json"):
    with open(filename) as json_data:
        data = json.load(json_data)
        #etc etc... Insert rest of code here

Upvotes: 2

Related Questions