python3 json,包含python3中JSON数据的不同压缩方法

So, I want to compress my JSON data using different compressor. I used this to compress the JSON.

import gzip

import JSON

with gzip.GzipFile('2.json', 'r') as isfile:

for line in isfile:

obj = json.loads(line)

which raises error.

raise OSError('Not a gzipped file (%r)' % magic)

OSError: Not a gzipped file (b'[\n')

I also tried direct compressing using.

zlib_data= zlib.compress(data)

which raises an error.

return lz4.block.compress(*args, **kwargs)

TypeError: a bytes-like object is required, not 'list'

So, Basically i want to compress a JSON using all the methods and to compute the time taken for the compression in different methods.

解决方案

On python2.7

it seems to be a problem of the type of your data

the data to compress should be a 'str' type

import gzip

import json

import lz4

import time

with gzip.GzipFile('data.gz','w') as fid_gz:

with open('data.json','r') as fid_json:

# get json as type dict

json_dict = json.load(fid_json)

# convert dict to str

json_str = str(json_dict)

# write string

fid_gz.write(json_str)

# check well maded

with gzip.GzipFile('data.gz','r') as fid_gz :

print(fid_gz.read())

even if gzip compression

gzip.zlib.compress(json_str,9)

even if lz4 compression

lz4.block.compress(json_str)

and time checking would be

# set start time

st = time.time()

# calculate elasped time

print(time.time() - st)

On python3.5

the difference between python2.7 and python 3 is the type of your data to compress

the data to compress should be a 'byte' type via bytes()

when making a .gz file

with gzip.GzipFile('data.gz','w') as fid_gz:

with open('data.json','r') as fid_json:

json_dict = json.load(fid_json)

json_str = str(json_dict)

# bytes(string, encoding)

json_bytes = bytes(json_str,'utf8')

fid_gz.write(json_bytes)

or just compress with gzip.compress(data, compresslevel=9)

# 'data' takes bytes

gzip.compress(json_bytes)

or just compress with zlib.compress(bytes, level=-1, /)

gzip.zlib.compress(json_bytes,9)

or just compress with lz4.bloc.compress(source, compression=0)

# 'source' takes both 'str' and 'byte'

lz4.block.compress(json_str)

lz4.block.compress(json_bytes)

the measuring time is on your intention.

cheers

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值