Skip to content

Instantly share code, notes, and snippets.

@Jongbhin
Last active February 27, 2021 06:21
Show Gist options
  • Save Jongbhin/3d389077dec878e38ab6ac8ba81b197e to your computer and use it in GitHub Desktop.
Save Jongbhin/3d389077dec878e38ab6ac8ba81b197e to your computer and use it in GitHub Desktop.
[csv read write] #python #csv

Using csv

# -*- coding: utf-8 -*-

import os
import csv

data_dir = 'some_dir'
input_file = os.path.join(data_dir, 'some_input_file.tsv')
output_file = os.path.join(data_dir, 'some_out_file.tsv')


def main():
    with open(input_file) as ifo, open(output_file, mode='w') as ofo:
        csv_reader = csv.reader((x.replace('\0', '') for x in ifo), delimiter='\t', quoting=csv.QUOTE_NONE)
        csv_writer = csv.writer(ofo, delimiter='\t', quoting=csv.QUOTE_NONE, quotechar='', escapechar='\\')  # for tsv
        # csv_reader = csv.reader(ifo, delimiter=',', quoting=csv.QUOTE_NONE)  # for csv
        # csv_writer = csv.writer(ofo, delimiter=',')  # for csv

        for counter, row in enumerate(csv_reader): # extract one row
            catalog_no, catalog_name = row[0:2]  # extract fields from a row
            csv_writer.writerow([catalog_no, catalog_name]) # write fields to writer


if __name__ == '__main__':
    main()
file_name = "boundary_exception.csv"

simple but use huge memory

with open(file_name) as fo:
    line_list = [line.rstrip('\n') for line in fo]
    print(line_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment