Skip to content

Instantly share code, notes, and snippets.

@svinota
Last active April 28, 2020 07:52
Show Gist options
  • Save svinota/b0e3c6323485bffbd7a7973fe67d4c14 to your computer and use it in GitHub Desktop.
Save svinota/b0e3c6323485bffbd7a7973fe67d4c14 to your computer and use it in GitHub Desktop.
'''
Usage::
cat momentary.txt | python translate.py >output.txt
'''
import sys
import csv
# create a CSV reader
reader = csv.reader(sys.stdin, dialect='excel-tab')
# create a writer to dump the values with correct quoting etc.
writer = csv.writer(sys.stdout,
dialect='excel-tab',
quoting=csv.QUOTE_NONNUMERIC)
# simply discard the header row
header = next(reader)
# write the new header
writer.writerow(['TIMESTAMP',
'EVENT',
'LOCUS.0',
'LOCUS',
'LOCUS.ORIG',
'NUMBER'])
# iterate the rest
for record in reader:
# fetch the number and check if it is a valid integer
# write back the converted value, so it will not be quoted
# in the output
try:
number = int(record[5])
record[5] = number
except ValueError:
continue
# deaggregate
for _ in range(number):
writer.writerow(record[:6])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment