Re: [Tutor] Newbie Trouble Processing SRT Strings In Text

2014-11-01 Thread Matt Varner
Alan G wrote: "This is a bad idea.  Instead, write your strings directly to o

o.write(s)

Print adds newlines automatically(unless you explicitly suppress
them). But printing to a file is messy compared to writing directly to
the file. (And also means you cant print debug messages while
developing your code!)"

>>> Thank you so much, Alan.  I had the feeling I was making it more difficult 
>>> on myself.  In fact, before I tried using stdout as a solution, I was 
>>> getting errors on my syntax because I was (apparently) trying to "call" the 
>>> list.  I'm sure I was putting syntax in the wrong order...but now I better 
>>> understand the use of file objects and lists (and strings!).  :D

===

Danny Yoo wrote: "You may want to look at existing parsers that people
have written. https://github.com/byroot/pysrt

>>>  That will definitely help out in the future once I get a little better 
>>> wrapping my head around programming with Python.  I would like to cut out 
>>> the middle man (app: Aegisub) if I could...eventually.  Now that my very 
>>> basic stumbling block is worked out, I can start building on it.  This will 
>>> likely come in handy.  :)

Danny Yoo wrote: "Rather than immediately print the string, you may
want to accumulate results in a list.  You can then do some processing
on your list of strings."

>>> Indeed, this is what I was trying with:

lns = f.readlines()

I was creating more trouble for myself by not having the basic syntax
order right, and then using stdout tied to my output file, using print
statements that were adding newlines, even though I was trying to edit
them out.

Thank you both again for your time!

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()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie Trouble Processing SRT Strings In Text

2014-11-01 Thread Peter Otten
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 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] all right students, what do we learn

2014-11-01 Thread Clayton Kirkwood
To prove that a little knowledge is a little stupid or something to that
effect:

#for key in key_list:

#print(key)

#if key not in key_list0:

#print("Error:", key, "not available, start again")

#get_new_list = True

#break

#else: get_new_list = False

 

Assume key_list0 = [('a', 'Ask'), ('y', 'Dividend Yield')]

 

The above worked when key_list was 'a y' or 'hhh'

 

Well, if a little cool was good why not use all of my new found knowledge of
comprehensions.

So I head down the path of all sorts of variations of the following. And I
mean all sorts. Instead of [reenter, key_columns] I used a single list.
Instead of the exec('reenter = True') I used a simple reenter=True (and an
eval and compile which I don't totally understand yet). Turns out that
python doesn't seem to like an assignment in the overall if clause - error.
Weird thing, entering   a y   would make the final print spit out (False,
'a') (False, 'o') but the entry of  hhh   caused the final print to spit out
[] (in the best of the various experiments. Which I don't quite understand.
The reason is because I set up the if statement to be True either way. With
an   a   it would go thru and create the proper output. But with  hhh   the
print prints out the Error:.., and I was hoping the assignment of True to
reenter would create a  True  which would make it out to the while. But it
won't. I think I have to just stay with the more verbose as above. Besides
after trying so many variations the comprehension was getting rather
kluge:<))

 

But  really grok comprehension a lot better.

 

 

reenter=True

while reenter:

reenter=False

key_list = input('Please enter space separated keys in the order you
want: ').split()

[reenter, key_columns ]=[(reenter, key) for key in key_list if ((key in
key_list0)) or (print("Error:", key, "not available, start again") and
exec('reenter = True', ))]

print(reenter,key_columns)

 

Clayton

 

You can tell the caliber of a man by his gun--c. kirkwood

 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor