[Newbie] Need help with quick text proggy.
Greg Krohn ("X", "@")
"gkrohnXvolucris.8m.com".replace
Thu Oct 10 03:18:37 EDT 2002
"Corey Woodworth" <schitz0boy at hotmail.com> wrote in message
news:24d8090a.0210091854.725f4c9f at posting.google.com...
> I havn't programmed in python in a while (not since 2.0) but I just
> found myself in a predicament where I really need to write a program
> and write it fast. Python still seems to be the best choice even
> though I'm very rusty and wasn't to good to start with. Here is my
> problem. I've got a text file that is a list of elements. a time
> followed by a number on each line. Like this for example:
>
> 1:46p 65
> 5:45a 99
> 2:36p 88
> 10:53a 79
> 5:10a 86
> 2:43p 169
> 10:14a 55
>
> I just found out that all my times are 6 hours off. (AARGH!) and I
> need some help writing a program that will update all those timestamps
> for me. Any help at all is appreciated, wheter it be links, some
> source, or whatever. Thank a bunch.
>
> Corey
How's this:
fin = file('your/file', 'r')
fout = file('your/other/file', 'w')
for line in fin:
try:
colon = line.find(':')
hour = int(line[:colon])
except ValueError:
print 'Invalid hour: %s' % line
continue
try:
space = line.find(' ')
minute = int(line[colon + 1: space - 1])
except ValueError:
print 'Invalid minute: %s' % line
continue
ampm = line[space - 1: space]
if ampm == 'p':
hour += 12
elif ampm != 'a':
print 'Invalid am/pm indentifier: %s' % line
continue
value = line[space + 1:]
hour += 6
hour = hour % 24
if hour > 12:
hour -= 12
ampm = 'p'
else:
ampm = 'a'
fout.write('%d:%d%s %s' % (hour, minute, ampm, value))
fin.close()
fout.close()
greg
More information about the Python-list
mailing list