0

I have a problem with this json file : Zips.json! .

I Didnt find a way to count the number of postal codes in each state. I tried many ways such as this :

file=open("C:\Users\Alex\Downloads/zips.json","r")
lines=json.loads(file.readline())
number=0
states=lines["state"]
for line in file:
    lines=json.loads(line)
    if lines ["state"]==states:
        number=number+1
    else:
        print u"states:",states, u"Number of codes: ",number
        states=lines["state"]
1
  • Thank for the reply, i add number=0 under my else but still not working. Commented Feb 10, 2016 at 8:47

1 Answer 1

2

The easiest way to do this is to use a collections.Counter:

import json
from collections import Counter
from operator import itemgetter

counter = None
state_getter = itemgetter('state')

with open('zips.json') as fh:
    zips_data = (json.loads(line) for line in fh)
    states_names = map(itemgetter('state'), zips_data)
    counter = Counter(states_names)

print(counter)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.