简体   繁体   中英

How to remove newlines but keep blank ones in a text file?

My question is essentially identical to the one found here , but I'd like to perform that operation using python 3. The text in my file looks something like this:

'''
Chapter One ~~ Introductory


The institution of a leisure class is found in its best development at
the higher stages of the barbarian culture; as, for instance, in feudal...
'''

Per numerous suggestions I've found, I have tried:

with open('veblen_txt_test.txt', 'r') as src:
    with open('write_new.txt', 'w') as dest:
       for line in src:
           if len(line) > 0:
               line = line.replace('\n', ' ') 
               dest.write(line)
           else:
               line = line + '\n\n'
               dest.write('%s' % (line))

But this returns:

'''
Chapter One ~~ Introductory  The institution of a leisure class is found in its best development at the higher stages of the barbarian culture; as, for instance, in feudal...
'''

The intended output is:

'''
Chapter One ~~ Introductory  


The institution of a leisure class is found in its best development at the higher stages of the barbarian culture; as, for instance, in feudal...
'''

I have tried using rstrip() :

with open('veblen_txt_test.txt', 'r') as src:
    with open('write_new.txt', 'w') as dest:
       for line in src:
           if len(line) > 0:
               line = line.rstrip('\n') 
               dest.write('%s%s' % (line, ' '))
           else:
               line = line + '\n\n'
               dest.write('%s' % (line))

But that yields the same result.

Most of the responses online address removing blank spaces, not keeping them; I have no doubt the solution is simple, but I've been trying different variations of the above code for about an hour and a half and just thought to ask the community. Thanks for your assistance!

If we change the len(line) > 0 to len(line) > 1 , it does the job. This is because \\n counts as 1 character. You'll also have to remove this line: line = line + '\\n\\n' as it adds 4 more extra lines (since there are two \\n in between Chapter One ~~ Introductory and The institution... .

Output:

Chapter One ~~ Introductory 

The institution of a leisure class is found in its best development at the higher stages of the barbarian culture; as, for instance, in feudal... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM