def import_phenolist(filepath, has_header):
# Return a list-of-dicts with the original column names, or integers if none.
# It'd be great to use pandas for this.
if not os.path.exists(filepath):
raise PheWebError("ERROR: unable to import {!r} because it doesn't exist".format(filepath))
# 1. try openpyxl.
phenos = _import_phenolist_xlsx(filepath, has_header)
if phenos is not None:
return phenos
with read_maybe_gzip(filepath) as f:
# 2. try json.load(f)
try:
return json.load(f)
except ValueError:
if filepath.endswith('.json'):
raise PheWebError("The filepath {!r} ends with '.json' but reading it as json failed.".format(filepath))
# 3. try csv.reader() with csv.Sniffer().sniff()
f.seek(0)
phenos = _import_phenolist_csv(f, has_header)
if phenos is not None:
return phenos
raise PheWebError("I couldn't figure out how to open the file {!r}, sorry.".format(filepath))
评论列表
文章目录