csv_utils.py 文件源码

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

项目:esys-pbi 作者: fsxfreak 项目源码 文件源码
def read_key_value_file(csvfile):
    """Reads CSV file, parses content into dict

    Args:
        csvfile (FILE): Readable file

    Returns:
        DICT: Dictionary containing file content
    """
    kvstore = {}  # init key value store
    first_line = csvfile.readline()
    if 'key' not in first_line or 'value' not in first_line:
        csvfile.seek(0)  # Seek to start if first_line is not an header
    dialect = csv.Sniffer().sniff(first_line, delimiters=',\t')
    reader = csv.reader(csvfile, dialect)  # create reader
    for row in reader:
        kvstore[row[0]] = row[1]
    return kvstore
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号