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.
- 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.
- I don't understand how gzip.open works here but it is given the name f_out.
- I don't understand writelines and remove here.