Hi, I'm new to Python and a beginner all round trying to interpret this code

Viewed 54
def list_to_csv(filename, data, header=None, skip_writing_list_header=False, dict_write_header=False, deli="\t", enc='utf-8', qc='"', method='w'):
    import csv

    with open(filename, method, encoding=enc) as csvfile:
        writer = csv.writer(csvfile, delimiter=deli, quotechar=qc, quoting=csv.QUOTE_MINIMAL)
        if header and skip_writing_list_header == False:
            writer.writerow(header)
        for line in data:
            if type(line) is dict:
                if header is None:
                    header=[k for k,v in line.items()]
                    if dict_write_header:
                        writer.writerow(header)
                line = ['' if x not in line else line[x] for x in header]
            writer.writerow(line)
    return filename


fn = list_to_csv(Location.folder_path + 'media/dumps/' + filename, updates, header=headers, qc="|")
            
            with open(fn, 'rb') as f_in, gzip.open(fn + '.gz', 'wb') as f_out:
                f_out.writelines(f_in)
            remove(fn)

Hello, I'm new to python and trying to understand the code above and how the 'with' code works here. I understand up to opening the file.

  1. open fn in read-only binary mode as give it a name f_in - the fn file is csv file that contains all the parameters mentioned in the variable fn.
  2. I don't understand how gzip.open works here but it is given the name f_out.
  3. I don't understand writelines and remove here.
1 Answers

First of all, it's good to always post the whole program. I don't think this is the whole thing because list_to_csv is not a standard python method, so we don't know how it's implemented. We don't know exactly what it does and can only assume it takes a list and writes it to a CSV file.

GZIP is a data compression tool. The CSV file created by list_to_csv is most likely compressed to GZIP as default, so it requires GZIP to decompress and open. You have these f_in and f_out names because these are file streams. the in stream reads, the out stream writes.

with keyword is a context manager, it opens the stream and ensures that the file is only accessed when the program actually needs to, and it closes it when its done with, so that other processes can access the file (concurrent access can lead to nasty errors)

writelines is a method of the output stream f_out, it will literally write the lines from the input file (which is being read by the input stream f_in into the output stream)

remove will remove the file passed from your system.

Related