Skip to content

Instantly share code, notes, and snippets.

@simonwagner
Last active May 30, 2023 23:54
Show Gist options
  • Save simonwagner/0ca407314bea9862ce6b15903fdcca87 to your computer and use it in GitHub Desktop.
Save simonwagner/0ca407314bea9862ce6b15903fdcca87 to your computer and use it in GitHub Desktop.
Convert Avid ALE files to CSV
#!/usr/bin/env python
from collections import OrderedDict
from itertools import dropwhile, izip
import csv
import argparse
import os.path
argparser = argparse.ArgumentParser()
argparser.add_argument("ale_file", metavar="ALE", type=argparse.FileType(mode="rb"))
argparser.add_argument("csv_file", metavar="CSV", type=argparse.FileType(mode="wb"), nargs="?", default=None)
def main():
args = argparser.parse_args()
columns, data = convert_ale_to_dict(args.ale_file)
args.ale_file.close()
if args.csv_file is not None:
csv_file = args.csv_file
else:
pre, ext = os.path.splitext(args.ale_file.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):
f_iter = iter(f)
f_iter = dropwhile(lambda line: line != "Column\n", f_iter)
column_line = next_or_none(f_iter)
if column_line is None:
raise Exception("No columns found")
column_names_line = next_or_none(f_iter)
if column_names_line is None:
raise Exception("No values for columns")
columns = column_names_line.replace("\n", "").split("\t")
f_iter = dropwhile(lambda line: line != "Data\n", f_iter)
data_line = next_or_none(f_iter)
if data_line is None:
raise Exception("No data found")
data = []
for data_values_line in f_iter:
values = data_values_line.replace("\n", "").split("\t")
values_dict = OrderedDict(izip(columns, values))
data.append(values_dict)
return (columns, data)
def dump_csv(f, columns, data):
dw = csv.DictWriter(f, fieldnames=columns)
dw.writeheader()
dw.writerows(data)
def next_or_none(iter):
try:
return next(iter)
except StopIteration:
return None
if __name__ == "__main__":
main()
@kangstarr
Copy link

kangstarr commented Oct 29, 2019

@simonwagner
@casausg
I updated the script to work properly in Python 3

  • updated the import from itertools to only import dropwhile since izip is now just zip natively in Python 3
  • changed the read and write by removing the "b" to accommodate for all strings being unicode in Python 3
  • changed the dropwhile to detect regardless of which newline character to make sure source ALE files can be written by any OS.
  • updated line to use zip instead of izip

...
from itertools import dropwhile
...
argparser.add_argument("ale_file", metavar="ALE", type=argparse.FileType(mode="r"))
argparser.add_argument("csv_file", metavar="CSV", type=argparse.FileType(mode="w"), nargs="?", default=None)
...
f_iter = dropwhile(lambda line: "Column" not in line.rstrip(), f_iter)
...
f_iter = dropwhile(lambda line: "Data" not in line.rstrip(), f_iter)
...
values_dict = OrderedDict(zip(columns, values))
...

@nickever
Copy link

Thanks Simon, this has been very useful. I've forked it and added the ability to iterate through multiple ALEs and handle Avid ALEs

@jaminmc
Copy link

jaminmc commented May 30, 2023

Howdy y'all! I enhanced this code and have if convert stuff from Alexa camera's to CSV for Resolve! Check it out:
https://github.com/jaminmc/ale2ResolveCSV

It also runs on the latest Python

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