Skip to content

Instantly share code, notes, and snippets.

@goddoe
Last active August 16, 2019 08:24
Show Gist options
  • Save goddoe/b7c47b537065e296402f3d2df293413c to your computer and use it in GitHub Desktop.
Save goddoe/b7c47b537065e296402f3d2df293413c to your computer and use it in GitHub Desktop.
tsv write and read using csv package.
import os
from collections import namedtuple
from ast import literal_eval
from os.path import dirname
export_path = "path/to/export"
# Read & Write using native python
# write tsv
os.makedirs(dirname(export_path), exist_ok=True)
wr = csv.writer(open(export_path, 'wt'), delimiter='\t')
for candi in candidate_list:
wr.writerow([candi, encoder.get_vector(candi).tolist()])
# read tsv
elem = namedtuple('query_space', "query embedding")
rdr = csv.reader(open(export_path, 'rt'), delimiter='\t')
r = [elem(row[0], literal_eval(row[1])) for row in rdr]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment