There is a list of dictionaries:
result = [{'1st_k': '1st_v', '2nd_k': '2nd_v', '3rd_k': '3rd_v'}, {'1st-k': '1st-v', '2nd-k': '2nd-v', '3rd-k': '3rd-v'}, {'1st.k': '1st.v', '2nd.k': '2nd.v', '3rd.k': '3rd.v'}] How is it more correct to write to a text file to get something like this:
1st_k: 1st_v 2nd_k: 2nd_v 3rd_k: 3rd_v 1st-k: 1st-v 2nd-k: 2nd-v 3rd-k: 3rd-v 1st.k: 1st.v 2nd.k: 2nd.v 3rd.k: 3rd.v That is, each pair of ключ: значение dictionary should be written on a new line, and between each dictionary should be an empty line. So far, I write this:
with open('test.txt', 'w', encoding='utf-8') as file: for record in result: for key, value in record.items(): file.write(f'\n{key}: {value}') file.write('\n') But I do not know how effective it is, plus at the beginning and at the end of the file, empty lines appear that I have to cut. Thanks in advance for your help.