简体   繁体   中英

How to write a python function to compress a CSV file using LZ4

Newbie here... I need to read and compress a CSV file using LZ4 and I have run into an expected error, the compress() function reads in bytes and the CSV file is incompatible. Is there a way to use LZ4 to compress an entire file or do I need to convert the CSV file into bit format and then compress it? If so how would I approach this?

import lz4.frame
import csv

file=open("raw_data_files/raw_1.csv")
type(file)
input_data=csv.reader(file)
compressed=lz4.frame.compress(input_data)

Error shows

Traceback (most recent call last):
  File "compression.py", line 10, in <module>
    compressed=lz4.frame.compress(input_data)
TypeError: a bytes-like object is required, not '_csv.reader'

You could do it like this:-

import lz4.frame

with open('raw_data_files/raw_1.csv', 'rb') as infile:
    with open('raw_data_files/raw_1.lz4', 'wb') as outfile:
        outfile.write(lz4.frame.compress(infile.read()))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM