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?
line.endswith("\.\n"), you need to useline.endswith(".\n"). The parameter tostr.endswithis not like that ofre(regular expressions)..doesn't carry any special meaning, so does not have to be escaped with `\`