0 length field in time string

MRAB python at mrabarnett.plus.com
Tue Aug 17 20:12:52 EDT 2010


Rodrick Brown wrote:
> Anyone know why I'm getting the following error when trying to parse the 
> following string is there a better method to use? 
> 
> #57=2010081708240065 - sample string passed to fmt_datetime
>  
> def fmt_datetime(tag57):
>     tag57   = tag57[3:len(tag57)]
>     year    = int ( tag57[0:4] )
>     mon     = int ( tag57[4:6] )
>     day     = int ( tag57[6:8])
>     hour    = int ( tag57[8:10] )
>     min     = int ( tag57[10:12>] )
>     sec     = int ( tag57[12:14>] )
>     msec    = int ( tag57[14:16] )
> 
You could use:

     dt = datetime.datetime.strptime(tag57[3 : 17], "%Y%m%d%H%M%S")

>     dt      = datetime.datetime(year,mon,day,hour,min,sec)
>     return '{:%Y-%m-%d %H:%M:%S}'.format(dt)
> 
>   File "./delta_delay.py", line 27, in fmt_datetime
>     return '{:%Y-%m-%d %H:%M:%S}'.format(dt)
> ValueError: zero length field name in format
>  
You haven't supplied an argument name or position (required in Python
2.6):

     return '{0:%Y-%m-%d %H:%M:%S}'.format(dt)



More information about the Python-list mailing list