Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A simple question about the indentation problem in txt

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading