Beginner String formatting question

J

JAMoore84

Hi all,

I am trying to write a simple program that will accept an integral
"time" input in the HHMMSS format and output a "HH:MM:SS" form. My
code is as follows:
========================
import string

def FormatTime(time):
'''Converts an HHMMSS string to HH:MM:SS format.'''

timeString = str(time) #converts the num to string

hours = [timeString[0], timeString[1]]
minutes = [timeString[2], timeString[3]]
seconds = [timeString[4], timeString[5]]

Ftime = "%s:%s:%s",(hours,minutes,seconds)
clock = Ftime.join()

return clock
===========================

when I run it from IDLE, I get this:
['1', '1', ':', '2', '2', ':', '3', '3']
['1', '1', ':', '2', '2', ':', '3', '3']

My questions-
1) Why is this function printing out twice?
2)It may be a formatting issue, but I want to have the output as
"HH:MM:SS", rather than having it broken out into each cell. I
thought the 'join' command would do this, but I must not be using it
properly/understanding something.
3)as a side note, I've noticed that the parameter "time" passed in
must be passed in as a string, otherwise I receive an error that
"time" is unsubscriptable. Am I unable to pass in an int and then
convert it to a string within the function with str()?

I've been at this for a while, so I may not be able to see the forest
through the trees at this point. I'd greatly appreciate any
suggestions or instruction on these mistakes.

Best,

Jimmy
 
M

Mark Tolonen

Hi all,

I am trying to write a simple program that will accept an integral
"time" input in the HHMMSS format and output a "HH:MM:SS" form. My
code is as follows:
========================
import string

def FormatTime(time):
'''Converts an HHMMSS string to HH:MM:SS format.'''

timeString = str(time) #converts the num to string

hours = [timeString[0], timeString[1]]
minutes = [timeString[2], timeString[3]]
seconds = [timeString[4], timeString[5]]

Ftime = "%s:%s:%s",(hours,minutes,seconds)
clock = Ftime.join()

return clock
===========================

when I run it from IDLE, I get this:
['1', '1', ':', '2', '2', ':', '3', '3']
['1', '1', ':', '2', '2', ':', '3', '3']

My questions-
1) Why is this function printing out twice?
2)It may be a formatting issue, but I want to have the output as
"HH:MM:SS", rather than having it broken out into each cell. I
thought the 'join' command would do this, but I must not be using it
properly/understanding something.
3)as a side note, I've noticed that the parameter "time" passed in
must be passed in as a string, otherwise I receive an error that
"time" is unsubscriptable. Am I unable to pass in an int and then
convert it to a string within the function with str()?

I've been at this for a while, so I may not be able to see the forest
through the trees at this point. I'd greatly appreciate any
suggestions or instruction on these mistakes.

Best,

Jimmy

Your code as displayed above doesn't work.
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "Format.py", line 13, in FormatTime
clock = Ftime.join()
AttributeError: 'tuple' object has no attribute 'join'Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "Format.py", line 13, in FormatTime
clock = Ftime.join()
AttributeError: 'tuple' object has no attribute 'join'

Make sure to copy/paste the exact working code.

Look at the 'time' and 'datetime' modules and the functions and methods
'strftime' and 'strptime' for displaying and parsing times.

--Mark
 
C

chaosgy

Hi all,

I am trying to write a simple program that will accept an integral
"time" input in the HHMMSS format and output a "HH:MM:SS" form. My
code is as follows:
========================
import string

def FormatTime(time):
'''Converts an HHMMSS string to HH:MM:SS format.'''

timeString = str(time) #converts the num to string

hours = [timeString[0], timeString[1]]
minutes = [timeString[2], timeString[3]]
seconds = [timeString[4], timeString[5]]

Ftime = "%s:%s:%s",(hours,minutes,seconds)
clock = Ftime.join()

return clock
===========================

when I run it from IDLE, I get this:

['1', '1', ':', '2', '2', ':', '3', '3']
['1', '1', ':', '2', '2', ':', '3', '3']

My questions-
1) Why is this function printing out twice?
2)It may be a formatting issue, but I want to have the output as
"HH:MM:SS", rather than having it broken out into each cell. I
thought the 'join' command would do this, but I must not be using it
properly/understanding something.
3)as a side note, I've noticed that the parameter "time" passed in
must be passed in as a string, otherwise I receive an error that
"time" is unsubscriptable. Am I unable to pass in an int and then
convert it to a string within the function with str()?

I've been at this for a while, so I may not be able to see the forest
through the trees at this point. I'd greatly appreciate any
suggestions or instruction on these mistakes.

Best,

Jimmy

import string


def FormatTime(time):
'''Converts an HHMMSS string to HH:MM:SS format.'''


timeString = "%06d" % (time, ) #converts the num to string

return timeString[:2] + ":" + timeString[2:4] + ":" +
timeString[4:];

this works
 
G

Gary Herron

Hi all,

I am trying to write a simple program that will accept an integral
"time" input in the HHMMSS format and output a "HH:MM:SS" form. My
code is as follows:
========================
import string

def FormatTime(time):
'''Converts an HHMMSS string to HH:MM:SS format.'''

timeString = str(time) #converts the num to string

hours = [timeString[0], timeString[1]]
minutes = [timeString[2], timeString[3]]
seconds = [timeString[4], timeString[5]]

Ftime = "%s:%s:%s",(hours,minutes,seconds)
clock = Ftime.join()

return clock
===========================

when I run it from IDLE, I get this:

['1', '1', ':', '2', '2', ':', '3', '3']
['1', '1', ':', '2', '2', ':', '3', '3']
The code you posted did not produce the output you are showing. You'll
have to be more careful with your posting if you expect to get a useful
answer.

Beyond that, there are a bundle of errors in the code that will prevent
it from working:

If you want to carve a string into substrings, use this kind of syntax
hours = timeString[0:2]
minutes = timeString[2:4]
seconds = timeString{4:6]

If you want to combine small strings into longer strings you've got
several choices:
output = hours + ':' + ...
or
output = "%s:%s:%s" % (hours, ...) #NOTICE the % operator between
the format string and value tuple

Since both of those produce a string as output, you have no need of a
join, BUT if you do decide to produce a list of strings that you want to
join into a longer string, you need to use join correctly:
parts = [hours,minutes,seconds]
output = ':'.join(parts)

Another error: If your time starts with an hour that is just a single
digit, then your string will be only 5 digits long (there will be no
leading zero). In that case, all the extraction of individual fields
based on indexing into the string will be off by one.
My questions-
1) Why is this function printing out twice?
Show us your real code.
2)It may be a formatting issue, but I want to have the output as
"HH:MM:SS", rather than having it broken out into each cell. I
thought the 'join' command would do this, but I must not be using it
properly/understanding something.
See the example above.
3)as a side note, I've noticed that the parameter "time" passed in
must be passed in as a string, otherwise I receive an error that
"time" is unsubscriptable. Am I unable to pass in an int and then
convert it to a string within the function with str()?
Of course you can pass any type into a function. Then you have to write
the program to operate correctly on whatever the input type is.
I've been at this for a while, so I may not be able to see the forest
through the trees at this point. I'd greatly appreciate any
suggestions or instruction on these mistakes.
My guess is that's why the code you show does not match the output you
present.
Best,

Jimmy

Gary Herron
 
J

JAMoore84

I apologize for the lack of details in my last post. This time
formatting program is a piece of a larger program I am trying to write
to de-clutter GPS transmission. I have a GPS receiver that transmits
its readings via bluetooth. I've been able to use pySerial and store
X number of bytes, then write that to a file (the next step will be
finding a way to write the stream to a file directly). The raw GPS
data is tranmitted in the NMEA format (http://vancouver-webpages.com/
peter/nmeafaq.txt):

$GPRMC,024830,V,,N,,E,,,260108,,,N*58
$GPVTG,,T,,M,,N,,K,N*2C
$GPGGA,024831,LAT_GOES_HERE,N,LONG_GOES_HERE,E,0,00,,,M,,M,,*61
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,3,1,09,09,,,37,13,,,00,18,,,00,23,,,00*75
$GPGSV,3,2,09,01,,,00,29,,,00,14,,,00,26,,,00*7A
$GPGSV,3,3,09,12,,,00,,,,,,,,,,,,*73
$GPRMC,024831,V,LAT_GOES_HERE,N,LONG_GOES_HERE,E,,,260108,,,N*59
$GPVTG,,T,,M,,N,,K,N*2C

the "$GPGGA" and "$GPRMC" lines are the ones that will give you your
Latitude and Longitude data --or the would if I had a signal.
All the entries are comma delimited, so I used the split(',') function
to parse the appropriate statements. Here's the code for that:
=============================
input = open('c:\sample_readout.txt','r') #location of the Raw GPS
data
output = open('c:\GPSout.txt', 'w')

output.write("Timestamp \t Latitude \t Longitude \t Velocity")

s = input.readlines()

for line in s:

if line.startswith('$GPGGA'):
InputTuple = line.split(',')

time = InputTuple[1]
lat = InputTuple[2]+' '+InputTuple[3]
long = InputTuple[4]+' '+InputTuple[5]

out = '\n '+ time + '\t\t' + lat + '\t\t\t' + long

output.writelines(out)

elif line.startswith('$GPRMC'):
InputTuple = line.split(',')

time = InputTuple[1]
lat = InputTuple[3]+' '+InputTuple[4]
long = InputTuple[5]+' '+InputTuple[6]

out = '\n '+ time + '\t\t' + lat + '\t\t\t' + long

output.writelines(out)

elif line.startswith('$GPVTG'): #this will give grounp speed in
knts and [7] gives kmph
InputTuple = line.split(',')

out = '\n ' + '\t\t' + InputTuple[5]

output.writelines(out)

input.close()
output.close()
========================

The time stamp for both the GPRMC and GPGGA lines will be stored in
InputTuple[1].

Right now, this program will read the GPS data file and pull out the
necessary information. I wanted the Format.py program to function as
a module so that I could call Format.FormatTime(time) to change the
HHMMSS GPS data to HH:MM:SS, for readability. The next step was to
write a FormatCoord function (within the Format module) to do the same
for lat/long data.

Now that I've explained the structure, the argument of
Format.FormatTime() will be InputTuple[1] (the timestamp). I want to
be able to call that function in the GPS program to read the contents
of InputTuple[1] and write the HH:MM:SS data to the file.

I've incorporated some of your recommendations into my program already
and it looks much better. Thanks to everyone for your suggestions.

Jimmy
 
J

John Machin

I apologize for the lack of details in my last post.

There is nothing to apologise for. Unlike some, you gave enough
information, without prompting, to get answers to your questions.
Don't go to the other extreme :)
This time
formatting program is a piece of a larger program I am trying to write
to de-clutter GPS transmission. I have a GPS receiver that transmits
its readings via bluetooth. I've been able to use pySerial and store
X number of bytes, then write that to a file (the next step will be
finding a way to write the stream to a file directly). The raw GPS
data is tranmitted in the NMEA format (http://vancouver-webpages.com/
peter/nmeafaq.txt):

$GPRMC,024830,V,,N,,E,,,260108,,,N*58
$GPVTG,,T,,M,,N,,K,N*2C
$GPGGA,024831,LAT_GOES_HERE,N,LONG_GOES_HERE,E,0,00,,,M,,M,,*61
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,3,1,09,09,,,37,13,,,00,18,,,00,23,,,00*75
$GPGSV,3,2,09,01,,,00,29,,,00,14,,,00,26,,,00*7A
$GPGSV,3,3,09,12,,,00,,,,,,,,,,,,*73
$GPRMC,024831,V,LAT_GOES_HERE,N,LONG_GOES_HERE,E,,,260108,,,N*59
$GPVTG,,T,,M,,N,,K,N*2C

the "$GPGGA" and "$GPRMC" lines are the ones that will give you your
Latitude and Longitude data --or the would if I had a signal.
All the entries are comma delimited, so I used the split(',') function
to parse the appropriate statements. Here's the code for that:
=============================
input = open('c:\sample_readout.txt','r') #location of the Raw GPS
data
output = open('c:\GPSout.txt', 'w')

Presumably you are running MS-DOS 2.0 or later, so you have a
hierarchical file system. Don't put files in your root directory!!!

Don't use single backslashes in Windows file names -- e.g. in 'foo
\noddy' the \n will be interpreted as a newline. Use one of the
following methods:
'c:\\GPS\\test1.txt'
r'c:\GPS\test1.txt'
'c:/GPS/test1.txt'

It's not really a good habit to have constant paths in your scripts
anyway, especially for output files.
output.write("Timestamp \t Latitude \t Longitude \t Velocity")

s = input.readlines()

for line in s:

Instead of the above two lines, do this:
for line in input:
if line.startswith('$GPGGA'):
InputTuple = line.split(',')

Try choosing meaningful names. It's not a tuple, it's a list.

And read this:
http://www.python.org/dev/peps/pep-0008/
time = InputTuple[1]
lat = InputTuple[2]+' '+InputTuple[3]
long = InputTuple[4]+' '+InputTuple[5]

out = '\n '+ time + '\t\t' + lat + '\t\t\t' + long

output.writelines(out)

elif line.startswith('$GPRMC'):
InputTuple = line.split(',')

You should write the code to parse the input format *once* and parse
it *correctly* -- at the moment the last field will contain
'data*checksum\n' instead of just 'data'.

Following is an example of one style of input parsing:

C:\junk>type nmea.py

sample = """
$GPRMC,024830,V,,N,,E,,,260108,,,N*58
$GPVTG,,T,,M,,N,,K,N*2C
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,3,1,09,09,,,37,13,,,00,18,,,00,23,,,00*75
$GPGSV,3,2,09,01,,,00,29,,,00,14,,,00,26,,,00*7A
$GPGSV,3,3,09,12,,,00,,,,,,,,,,,,*73
$GPVTG,,T,,M,,N,,K,N*2C
$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
"""

def unpack_nmea_message(line):
line = line.rstrip() # remove any newline etc
if not line:
return [] # empty line
if line[0] != '$':
raise Exception('NMEA line %r: should start with $' % line)
try:
apos = line.rindex('*')
except ValueError:
raise Exception('NMEA line %r: missing checksum?' % line)
try:
cksum = int(line[apos+1:], 16)
except ValueError:
raise Exception('NMEA line %r: checksum not hex' % line)
if not(0 <= cksum <= 255):
raise Exception(
'NMEA line %r: checksum not in range 0-255 inclusive'
% line)
calc_cksum = 0
data = line[1:apos]
for c in data:
calc_cksum = calc_cksum ^ ord(c)
if calc_cksum != cksum:
print calc_cksum, cksum, repr(chr(calc_cksum ^ cksum))
raise Exception('NMEA line %r: invalid checksum' % line)
fields = data.split(',')
# maybe check for minimum number of fields
return fields

if __name__ == '__main__':
for x, line in enumerate(sample.splitlines()):
print 'Line %3d: %r' % (x+1, line)
print ' : %r'% unpack_nmea_message(line)

C:\junk>nmea.py
Line 1: ''
: []
Line 2: '$GPRMC,024830,V,,N,,E,,,260108,,,N*58'
: ['GPRMC', '024830', 'V', '', 'N', '', 'E', '', '', '260108',
'', '', 'N']
Line 3: '$GPVTG,,T,,M,,N,,K,N*2C'
: ['GPVTG', '', 'T', '', 'M', '', 'N', '', 'K', 'N']
Line 4: '$GPGSA,A,1,,,,,,,,,,,,,,,*1E'
: ['GPGSA', 'A', '1', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '']
Line 5: '$GPGSV,3,1,09,09,,,37,13,,,00,18,,,00,23,,,00*75'
: ['GPGSV', '3', '1', '09', '09', '', '', '37', '13', '', '',
'00', '18', '', '', '00', '23', '', '', '00']
Line 6: '$GPGSV,3,2,09,01,,,00,29,,,00,14,,,00,26,,,00*7A'
: ['GPGSV', '3', '2', '09', '01', '', '', '00', '29', '', '',
'00', '14', '', '', '00', '26', '', '', '00']
Line 7: '$GPGSV,3,3,09,12,,,00,,,,,,,,,,,,*73'
: ['GPGSV', '3', '3', '09', '12', '', '', '00', '', '', '',
'', '', '','', '', '', '', '', '']
Line 8: '$GPVTG,,T,,M,,N,,K,N*2C'
: ['GPVTG', '', 'T', '', 'M', '', 'N', '', 'K', 'N']
Line 9: '$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,
46.9,M,,*47'
: ['GPGGA', '123519', '4807.038', 'N', '01131.000', 'E', '1',
'08', '0.9', '545.4', 'M', '46.9', 'M', '', '']

C:\junk>

HTH,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top