pythoncsvcompressionlz4

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'

Solution

  • 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()))