#Version:v1.0 #Data:2021/01/01 #Author:Jam #Description: This is a example of csv commit
利用以下函数可以从csv文件中以字典的形式获取注释中的key-value
1 2 3 4 5 6 7 8 9 10 11 12 13
defcsv_read_attrs(filename): s = '' fobj = open(filename) whileTrue: line = fobj.readline() ifnot line: break s += line attrs = re.findall(r'#(.*)', s) keys = [x.split(':')[0].strip() for x in attrs] values = [x.split(':')[1].strip() for x in attrs] attrs = dict(zip(keys,values)) return attrs
将上述注释保存为test.csv文件,调用csv_read_attrs函数解析注释信息
1 2 3 4
>>> import csv_read_attrs from csvAttrs >>> attrs = csv_read_attrs('test.csv') >>> attrs {'Version': 'v1.0', 'Data': '2021/01/01', 'Author': 'Jam', 'Description': 'This is a example of csv commit'}