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
评论列表
文章目录