__init__.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:pheweb 作者: statgen 项目源码 文件源码
def _import_phenolist_xlsx(filepath, has_header):
    import openpyxl
    try:
        wb = openpyxl.load_workbook(filepath)
        assert len(wb.worksheets) == 1
        sheet = wb.worksheets[0]
        rows = [[cell.value for cell in row] for row in sheet.rows]
        num_cols = len(rows[0])
        if has_header:
            fieldnames, rows = rows[0], rows[1:]
            if any(fieldname is None or fieldname == '' for fieldname in fieldnames):
                if has_header == 'augment':
                    fieldnames = [i if fieldname is None else fieldname for i, fieldname in enumerate(fieldnames)]
                else:
                    raise PheWebError('bad xlsx header')
            assert len(set(fieldnames)) == len(fieldnames), fieldnames
        else:
            fieldnames = list(range(num_cols))
        return [{fieldnames[i]: row[i] for i in range(num_cols)} for row in rows]
    except openpyxl.utils.exceptions.InvalidFileException:
        if filepath.endswith('.xlsx'):
            raise PheWebError("The filepath {!r} ends with '.xlsx' but reading it as an excel file failed.".format(filepath))
        return None
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号