Skip to content

Instantly share code, notes, and snippets.

@ptsefton
Created August 12, 2014 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptsefton/de681f7ed71d89178155 to your computer and use it in GitHub Desktop.
Save ptsefton/de681f7ed71d89178155 to your computer and use it in GitHub Desktop.
Quick incomplete sample code for extracting CSV and CKAN-flavoured metadata from a TOA5 file
import csv
import StringIO
filestem = "WTC12_Table2_20140831"
in_file = "/Users/pt/Downloads/%s.dat" % filestem
out_file = "/Users/pt/Downloads/%s.csv" % filestem
def TOA52csv(in_file, out_file):
data = csv.reader(open(in_file))
writer = csv.writer(open(out_file, "w"))
print dir(data)
metadata_row = data.next()
dataset_dict = {
"name" : filestem,
"notes" : filestem,
"extras" : [{"key": "Sample Interval", "value" : metadata_row[1]}]
}
header1 = data.next()
header2 = data.next()
header3 = data.next()
i = 0
new_header = []
for field in header1:
new_header.append("%s :: %s :: %s" % (field, header2[i], header3[i]))
i = i + 1
#print new_header
writer.writerow(new_header)
for row in data:
#print row
writer.writerow(row)
return dataset_dict
print TOA52csv(in_file, out_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment