[Tutor] Newbie Trouble Processing SRT Strings In Text
Peter Otten
__peter__ at web.de
Sat Nov 1 09:31:56 CET 2014
Matt Varner wrote:
> This result works perfectly (REMs removed):
>
> f = open('tmp.txt', 'r')
> o = open('result.txt', 'w')
> lns = f.readlines()
> f.close()
> for line in lns:
> if ".\n" in line:
> a = line.replace('.\n','. ')
> o.write(a)
> else:
> a = line.strip('\n')
> o.write(a + " ")
> o.close()
Congratulations!
Two remarks:
- Often you don't need to load the lines of a file in a list with
readlines(), you can iterate over the file directly:
for line in f:
...
- Python has a nice way to open a file and make sure that it's closed after
usage, the with statement:
with open("tmp.txt") as f:
for line in f:
...
print("at this point the file is closed")
Finally, you may also take a look into the textwrap module in the standard
library:
>>> import textwrap
>>> with open("tmp.txt") as f:
... print(textwrap.fill(f.read(), width=50, fix_sentence_endings=True))
...
# Exported by Aegisub 3.2.1 [Deep Dive] [CSS
Values & Units Numeric and Textual Data Types
with Guil Hernandez] In this video, we'll go over
the common numeric and textual values that CSS
properties can accept. Let's get started. So,
here we have a simple HTML page containing a div
and a paragraph element nested inside. It's
linked to a style sheet named style.css and this
is where we'll be creating our new CSS rules.
See <https://docs.python.org/dev/library/textwrap.html>
More information about the Tutor
mailing list