I am trying to count the lines in a JSON file.
Click HERE to access my JSON file .
I tried to use the below code to count the lines.
input = open("json/world_bank.json")
i=0
for l in input:
i+=1
print(i)
But the above code is throwing a UniCodeDecode Error as shown below.
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-17-edc88ade7225> in <module>()
2
3 i=0
----> 4 for l in input:
5 i+=1
6
C:UsersSubbi ReddyAppDataLocalContinuumAnaconda3libencodingscp1252.py in decode(self, input, final)
21 class IncrementalDecoder(codecs.IncrementalDecoder):
22 def decode(self, input, final=False):
---> 23 return codecs.charmap_decode(input,self.errors,decoding_table)[0]
24
25 class StreamWriter(Codec,codecs.StreamWriter):
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 3979: character maps to <undefined>
Then i included encoding parameter in open function as shown below.
input = open("json/world_bank.json",encoding="utf8")
Then it started working and giving output as 500.
As far as i know python open should consider "utf8" as default encoding.
Where i am going wrong in here.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…