我正在使用 csv.DictReader() 读取文件。它实际上返回一个字典列表,而不是单个字典。如何强制它返回单个字典或如何合并它返回的字典列表?
def agilent_e8361c_pna_read(file_loc):
'''
Load the '.s2p' file in to a dictionary.
'''
with open(file_loc) as f:
# define the fields in the Agilent '.s2p' file
col_names = ["f","s11","arg_s11","s21","arg_s21","s12","arg_s12","s22","arg_s22"]
# read the data into a dictionary
s2p_dicts = csv.DictReader(itertools.ifilter(n_input.is_comment, f), fieldnames=col_names, delimiter=' ')
return s2p_dict
理想情况下,数据最初会被读入单个字典,并且永远不需要合并。它是一组数据。这些列属于一起并且没有完整集或连贯子集是无意义的。如果 DictReader 不能“以python 方式”实现这一壮举,我将决定只合并字典列表。这不应该是科学家和程序员都想用数据集做的一件不寻常的事情。