I'm trying to log data from some sensors to the dictionary, and then convert the dictionary to string in order to save them as a .csv file on the sd card.
The dictionary has 13 items, and each of them has a list in which there are about 60-80 data points (float number). I generated mock data with below code:
Code: Select all
dataset = {
'item0': [a for a in range(60)],
'item1': [a for a in range(60)],
'item2': [a for a in range(60)],
'item3': [a for a in range(60)],
'item4': [a for a in range(60)],
'item5': [a for a in range(60)],
'item6': [a for a in range(60)],
'item7': [a for a in range(60)],
'item8': [a for a in range(60)],
'item9': [a for a in range(60)],
'item10': [a for a in range(60)],
'item11': [a for a in range(60)],
'item12': [True if a % 2 == 0 else False for a in range(60)]
}
Code: Select all
def dict_to_csv(the_dict, omit_keys=False, delimiter=',', end='\r\n', sort_keys=True):
"""
Convert dictionary to string in csv format
The delimiter will be stripped from the content to avoid format error.
:param the_dict: dictionary to be converted
:param delimiter: string, default to be comma
:param end: string, line-break, default to be '\r\n'
:param sort_keys: boolean, whether or not sort the keys, in order to align
multiple dictionaries which share the same keys
:param omit_keys: boolean, whether or not omit keys. in the case of combining
multiple dictionaries which share the same keys, only the 1st dict needs
to show the keys.
:return: String
"""
def cleared_value(the_value, the_delimiter):
if the_value is None:
the_value = ""
if the_value is True:
the_value = "True"
if the_value is False:
the_value = "False"
return str(the_value).strip(the_delimiter)
data_len = 0
for data_list in the_dict.values():
data_len = len(data_list)
break
csv_content = ''
key_list = [key for key in the_dict.keys()]
if sort_keys:
key_list.sort()
if not omit_keys:
for key in key_list:
key = cleared_value(key, delimiter)
csv_content += (key + delimiter)
csv_content = csv_content.rstrip(delimiter)
csv_content += end
for i in range(data_len):
for key in key_list:
value = cleared_value(the_dict[key][i], delimiter)
csv_content += (value + delimiter)
csv_content = csv_content.rstrip(delimiter)
csv_content += end
return csv_content
Code: Select all
def run_test():
start = utime.ticks_ms()
content = dict_to_csv(dataset)
convert_time = utime.ticks_diff(utime.ticks_ms(), start)
print('convertion took ' + str(convert_time) + ' ms')
Any suggestion that I can refine the code to optimize the performance?
The board I'm using is an ESP32 wrover, and the firmware is the lastest unstable build.
Thanks in advance.
Kaiyuan