1

I have a .json file which contains a JSON item on every line. I want to try and count how many JSON items I have within the file using Python. I am currently just counting the lines using the following code

count =0
with open(file) as f:
    for line in f:
        count+=1

This seems like an unefficient way of doing it though. Is there a more efficient way of either calculating the number of JSON items in a .json file or counting the number of lines within a file?

illwalkwithyou
  • 349
  • 1
  • 6
  • 21

1 Answers1

3

Edit to fix my wrong doing:

This is a one liner for counting lines in file.

num = sum(1 for line in open(filename))
Lampshady
  • 102
  • 4