Python使用惰性方法读取1gb json文件

时间:2017-01-11 07:34:52

标签: python json

BroadcastReceiver PowerStatusReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, ifilter);

        // Are we charging / charged?
        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                     status == BatteryManager.BATTERY_STATUS_FULL;
    }
};  

我有1gb的大文件,所以我尝试了上面的方法来读取,它将值打印为字符串,但不能读取为json,因为它的大小为2k

我试过以下代码

 import json

 def read_in_chunks(file_object, chunk_size=2048):
  """Lazy function (generator) to read a file piece by piece.
  Default chunk size: 2k."""
  while True:
    data = file_object.read(chunk_size)
    if not data:
        break
    yield data


  f = open('productfeed.json','r')
  for piece in read_in_chunks(f):
      print piece

得到这样的错误“ ValueError:从第1行第1012行开始的未终止字符串(字符1011 ) “

不知道如何继续进行,请有人帮助我,不允许使用 ijson

0 个答案:

没有答案