我试图计算JSON文件中的行.
Click HERE to access my JSON file .
我尝试使用下面的代码来计算行数.
input = open("json/world_bank.json")
i=0
for l in input:
i+=1
print(i)
但上面的代码抛出了UniCodeDecode错误,如下所示.
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
in ()
2
3 i=0
----> 4 for l in input:
5 i+=1
6
C:\Users\Subbi Reddy\AppData\Local\Continuum\Anaconda3\lib\encodings\cp1252.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
然后我在open函数中包含了编码参数,如下所示.
input = open("json/world_bank.json",encoding="utf8")
然后它开始工作并输出为500.
据我所知,python open应该将“utf8”视为默认编码.
我在这里错了.