def sav_dic(self):
f2 = open('my_dic.txt', 'a')
for j in range(len(re_doc2)):
f2.write(re_doc2[j]+':'+def_lst[j])
f2.close()
Above is a function within a class. Also, the content is already written in my_dic.txt.
A:Contents of B
B:Contents of B
I want to add content through the above function. But there was a problem here. I’m going to add the contents from C to E. The ideal situation I hope for is as follows.
A:Contents of B
B:Contents of B
C:Contents of C
D:Contents of D
E:Contents of E
However, if I use the above function, it comes out as follows.
A:Contents of B
B:Contents of BC:Contents of C
D:Contents of D
E:Contents of E
I tried writing an escape code again, but this time it comes out like this.
def sav_dic(self):
f2 = open('my_dic.txt', 'a')
for j in range(len(re_doc2)):
f2.write('\n'+re_doc2[j]+':'+def_lst[j])
f2.close()
A:Contents of B
B:Contents of B
C:Contents of C
D:Contents of D
E:Contents of E
I’m quite bothered by this. How can solve this?
>Solution :
You can write a ‘\n’ before you write new lines to f2.
def sav_dic(self):
f2 = open('my_dic.txt', 'a')
f2.write('\n')
for j in range(len(re_doc2)):
f2.write(re_doc2[j]+':'+def_lst[j])
f2.close()