2

Say I have a lorem ipsum text like this:

Lorem ipsum dolor sit amet, ut his semper vivendo
apeirian, graece electram sea an, vix assum euismod
luptatum ei.

Duo an diceret delicata referrentur. Cum ex mazim
legendos corrumpit, eu fuisset omnesque qui,
sit cu populo possit placerat. Lorem dolore ut has,
in his ancillae incorrupte. In esse inani nec.

And I want to make this into two paragraphs with two lines, something like this:

Lorem ipsum dolor sit amet, ut his semper vivendo apeirian, graece electram sea an, vix assum euismod luptatum ei.

Duo an diceret delicata referrentur. Cum ex mazim legendos corrumpit, eu fuisset omnesque qui, sit cu populo possit placerat. Lorem dolore ut has, in his ancillae incorrupte. In esse inani nec.

So I wrote my code like this:

f = open('lorem ipsum.txt', 'r')
o = open('output.txt', 'w')
for line in f:
    if line.endswith("\.\n") == True:
        o.write(line)
    else:
        line = line[:-1] + " "
        o.write(line)

Second part of getting rid of newline where it's not necessary works, but the code doesn't seem to recognize dot-newline characters very well. The code ends up writing all text into one line like this:

Lorem ipsum dolor sit amet, ut his semper vivendoapeirian, graece electram sea an, vix assum euismodluptatum ei. Duo an diceret delicata referrentur. Cumex mazim legendos corrumpit, eu fuisset omnesque qui, sit cu populo possit placerat. Lorem dolore ut has, inhis ancillae incorrupte. In esse inani nec.

What am I doing wrong?

4
  • 1
    Instead of line.endswith("\.\n"), you need to use line.endswith(".\n"). The parameter to str.endswith is not like that of re (regular expressions). . doesn't carry any special meaning, so does not have to be escaped with `\` Commented Mar 22, 2015 at 4:57
  • @SakshamVarma I swear, last time I tried that it didn't work. Now it suddenly does. Thank you very much anyway. Also, what's the reason for that? Commented Mar 22, 2015 at 5:01
  • @FJSK your are confusing string matching with pattern matching. Commented Mar 22, 2015 at 5:02
  • @Kay Ah.. I knew that... Rookie mistake, haha. Thank you. Commented Mar 22, 2015 at 5:03

1 Answer 1

1

Try splitting the content whenever there is period along with new line. Replace the other newlines and join them again with newline.

f = open('lorem ipsum.txt', 'r')
o = open('output.txt', 'w')
contents = f.read()
new = contents.split('.\n')
o.write('\n'.join([content.replace('\n', '') for content in new]))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.