1

This might be a simple question but I just started Python and don't know why this is not working. I am trying to remove the empty lines from a text and nothing seems to work.

I have the following sample text

The Project Gutenberg EBook of Alice in Wonderland, by Lewis Carroll

This eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever.  You may copy it, give it away or
re-use it under the terms of the Project Gutenberg License included
with this eBook or online at www.gutenberg.org


Title: Alice in Wonderland

Author: Lewis Carroll

Illustrator: Gordon Robinson

Release Date: August 12, 2006 [EBook #19033]

Language: English

Character set encoding: ASCII

*** START OF THIS PROJECT GUTENBERG EBOOK ALICE IN WONDERLAND ***

and I need the outcome to be a long line of text like so:

The Project Gutenberg EBook of Alice in Wonderland, by Lewis Carroll This eBook is for the use of anyone anywhere at no cost and with almost no restrictions whatsoever.  You may copy it, give it away or re-use it under the terms of the Project Gutenberg License included with this eBook or online at www.gutenberg.org Title: Alice in Wonderland Author: Lewis Carroll Illustrator: Gordon Robinson Release Date: August 12, 2006 [EBook #19033] Language: English Character set encoding: ASCII *** START OF THIS PROJECT GUTENBERG EBOOK ALICE IN WONDERLAND ***

I have tried

text=open("sample.txt","r")

for line in text:
    line = line.rstrip()
    print(line)

and .strip() as well but they don't do anything to the text. Is there a reason this does not work? I would like the code to be a one liner or something I can save as a variable because I need the outcome later. This is part of a larger project and I can't get past this.

3 Answers 3

3

You need to avoid the default behaviour of print() which is to output newline. You achieve as follows:-

with open('sample.txt') as txtfile:
    for line in txtfile:
        print(line.strip(), end='')
    print()

For this particular case you could also do this:-

with open('sample.txt') as txtfile:
  contents = txtfile.read().replace('\n', '')
  print(contents)
Sign up to request clarification or add additional context in comments.

1 Comment

Hi! Firstly, thank you for your answer. I tried it and it worked! However, with the command made inside print(), I don't know if I can save the outcome as a variable to use the string for another iteration. Is there a way to do that?
1
text=open("sample.txt")
print(' '.join([line.strip() for line in text]))

You can also use a variable to hold value and print, instead of directly printing.

single = ' '.join([line.strip() for line in text])
print (single)

Comments

0

You can do this by replacing the newlines with spaces:

string=string.replace("\n\n"," ")

or using .splitlines(), and then joining them back together:

stringlist=string.splitlines()
string= " ".join(stringlist)

note: In the first example, I replaced every two newlines with a space, as the example has two newlines seperating each line. Use "\n" if you have only one newline seperating each line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.