-
-
Save nickever/572f27be798410c5938367f0715ea443 to your computer and use it in GitHub Desktop.
Convert Avid ALE files to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from collections import OrderedDict | |
from itertools import dropwhile | |
import csv | |
import argparse | |
import os.path | |
argparser = argparse.ArgumentParser() | |
argparser.add_argument("ale_file", metavar="ALE", type=argparse.FileType(mode="r"), nargs="+") | |
def main(): | |
args = argparser.parse_args() | |
for f in args.ale_file: | |
columns, data = convert_ale_to_dict(f) | |
f.close() | |
if columns is not None: | |
pre, ext = os.path.splitext(f.name) | |
csv_path = pre + ".csv" | |
csv_file = open(csv_path, mode="wb") | |
dump_csv(csv_file, columns, data) | |
csv_file.close() | |
def convert_ale_to_dict(f): | |
try: | |
f_iter = f.read().splitlines() | |
f_iter = dropwhile(lambda line: "Column" not in line.rstrip(), f_iter) | |
column_line = next_or_none(f_iter) | |
if column_line is None: | |
raise Exception("No columns found " + f.name) | |
column_names_line = next_or_none(f_iter) | |
if column_names_line is None: | |
raise Exception("No values for columns " + f.name) | |
columns = column_names_line.replace("\n", "").split("\t") | |
f_iter = dropwhile(lambda line: "Data" not in line.rstrip(), f_iter) | |
data_line = next_or_none(f_iter) | |
if data_line is None: | |
raise Exception("No data found " + f.name) | |
data = [] | |
for data_values_line in f_iter: | |
values = data_values_line.replace("\n", "").split("\t") | |
values_dict = OrderedDict(zip(columns, values)) | |
data.append(values_dict) | |
return columns, data | |
except Exception as e: | |
print(e) | |
return None, None | |
def dump_csv(f, columns, data): | |
dw = csv.DictWriter(f, fieldnames=columns) | |
dw.writeheader() | |
dw.writerows(data) | |
def next_or_none(iter_obj): | |
try: | |
return next(iter_obj) | |
except StopIteration: | |
return None | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
awk '(NR == 1) || (FNR > 1)' file*.csv > bigfile.csv
to combine all csv in terminal.