1

I have 10 json files called Herald500_2005, Herald500_2006.... Herald500_2015. I am trying to carry the same search of keywordswords in each file. Instead of doing it one by one I would like to be able to do it in a loop. So far I have tried the following code:

for i in range(5,15):
    df = pandas.DataFrame([json.loads(l) for l in open('Herald500_200i.json')])
# Parse dates and set index
    df.date = pandas.to_datetime(df.date)
    df.set_index('date', inplace=True)
# match keywords
    matchingbodies = df[df.body.str.contains("|".join(keywords3))&df.body.str.contains("|".join(keywords2))&df.body.str.contains("|".join(keywords1))].body
# Count by month
    counts = matchingbodies.groupby(lambda x: x.month).agg(len)

    print "TH 200i" 
    print counts

By running this code I am getting the following error:

<ipython-input-9-76f2d2649df0> in <module>()
      1 for i in range(5,15):
----> 2     df = pandas.DataFrame([json.loads(l) for l in open('Herald500_200i.json')])
      3 # Parse dates and set index
      4     df.date = pandas.to_datetime(df.date)
      5     df.set_index('date', inplace=True)

IOError: [Errno 2] No such file or directory: 'Herald500_200i.json'

Any idea how to correct the code?

Kind regards

1
  • You have 11 files, not 10. Commented Mar 14, 2016 at 16:32

1 Answer 1

8

You should use string formatting to get the i value into the string:

open('Herald500_200%d.json' % i) 

Or:

open('Herald500_200{0}.json'.format(i)) 

Or, since these are years and to simplify handling the leading zero formatting problem, just loop over the years directly:

for year in range(2005, 2016):  # note the 2016 here - the upper bound is non-inclusive
    df = pandas.DataFrame([json.loads(l) for l in open('Herald500_%d.json' % year)])
Sign up to request clarification or add additional context in comments.

2 Comments

Works excelent, but the only problem is that when reaching to 2010, it tries to get 20010, I guess I could fix it with for i in range(05,15) and open('Herald500_20%d.json' % i)
Works great @alecxe!

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.