1

I have a problem where I have a json file of businesses open and closed. I need to specify the number of open businesses which is why I did this. But it returns 'none'. Note that I have to use functions. Also I'm not using a simple counter because I have to actually delete the closed business, because I have to do more stuff with them. This is not a duplicate because I tried what the other post says and it gives me 0.

Here is what an entry of the json file looks like:

{
    "business_id":"1SWheh84yJXfytovILXOAQ",
    "name":"Arizona Biltmore Golf Club",
    "address":"2818 E Camino Acequia Drive",
    "city":"Phoenix",
    "state":"AZ",
    "postal_code":"85016",
    "latitude":33.5221425,
    "longitude":-112.0184807,
    "stars":3.0,
    "review_count":5,
    "is_open":0,
    "attributes":{
        "GoodForKids":"False"
    },
    "categories":"Golf, Active Life",
    "hours":null
}
import json
liste_businesses=[]
liste_open=[]
def number_entries(liste_businesses):
    with open ('yelp.txt') as file:
        for line in file:
            liste_businesses.append(json.loads((line)))
    return (len(liste_businesses))
def number_open(liste_businesses):
    for e in range (len(liste_businesses)):
        if 'is_open' not in liste[e]:
           liste_open=liste_businesses.remove(liste[e])
        if int(liste[e]['is_open'])==int(0):
            liste_open=liste_businesses.remove(liste[e])
print(number_open(liste_businesses))
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anthony
  • 25
  • 7
  • Possible duplicate of [Python: Removing list element while iterating over list](https://stackoverflow.com/questions/6022764/python-removing-list-element-while-iterating-over-list) – snakecharmerb Nov 17 '19 at 17:22
  • The use of dictionaries doesn't make it different @snakecharmerb? – Anthony Nov 17 '19 at 17:33
  • Not really - they're just elements in the list. But feel free to experiment with possible solutions before choosing to accept the duplicate (or not). – snakecharmerb Nov 17 '19 at 17:35
  • i deletet my answer. because on mine, you can run into "invalid key" error.. use the one from chathan! and... don't forget to upvote his answer :-) – tgyger Nov 17 '19 at 18:54
  • ok thanks, i still don't know if it works because it is still running – Anthony Nov 17 '19 at 19:08

1 Answers1

1

Unless you're dealing with memory constraints, it's probably simpler to just iterate over your list of businesses and select the open ones:

def load_businesses():
    businesses = []
    with open('yelp.txt') as file:
        for line in file:
            businesses.append(json.loads(line))

    #  More idiomatic to return a list than to modify global state
    return businesses


def get_open_businesses(businesses):
    # Make a new list rather than modifying the old one
    open_businesses = []
    for business in businesses:
        if business.get('is_open', '0') != '0':
            open_businesses.append(business)

    return open_businesses


businesses = load_businesses()
open_businesses = get_open_businesses(businesses)
print(len(open_businesses))

If you wanted to use a list comprehension for the open businesses:

[b for b in businesses if b.get('is_open', '0') != '0']
Chathan Driehuys
  • 1,173
  • 3
  • 14
  • 28
  • Which part doesn't work? Is `businesses` populated correctly? You can use a debugger, logging, or print statements to see what's happening in the program at any specific time to see when the program differs from what you expect. – Chathan Driehuys Nov 17 '19 at 18:11
  • Thank you, I wanted to add my 'not in' thing, would it work I just put it as 'or' at the end of your list comprehension? – Anthony Nov 17 '19 at 18:18
  • I added an example of your logic. If there's more logic, I tend to pull it out into a full for loop rather than a list comprehension. – Chathan Driehuys Nov 17 '19 at 18:23
  • Is it weird that it is still running from when I tried what 13 minutes ago? – Anthony Nov 17 '19 at 18:32
  • Unless this text file is absolutely massive, yes. Try using a debugger, logging messages, or print statements to figure out what it has executed and where the program is getting stuck. – Chathan Driehuys Nov 17 '19 at 18:34