Skip to content

Instantly share code, notes, and snippets.

@slowkow
Created October 21, 2015 19:26
Show Gist options
  • Save slowkow/cbde7f84f2f45e4cf038 to your computer and use it in GitHub Desktop.
Save slowkow/cbde7f84f2f45e4cf038 to your computer and use it in GitHub Desktop.
Merge two files with Python.
# https://www.biostars.org/p/162853
cache = {}
f2 = open('f2')
header2 = f2.next()
for line in f2:
fields = line.strip().split()
snp = fields[0]
cache[snp] = '\t'.join(fields[1:])
f1 = open('f1')
header1 = f1.next()
print header1.strip() + '\t' + '\t'.join(header2.split()[1:])
for line in f1:
line = line.strip()
fields = line.split()
snp = fields[0]
if snp in cache:
print '{}\t{}'.format(line, cache[snp])
else:
print '{}\t0\t0\t0\t0\t0'.format(line)
@slowkow
Copy link
Author

slowkow commented Nov 27, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment