Date2Epoch script

MRAB python at mrabarnett.plus.com
Mon Jun 13 11:27:55 EDT 2011


On 13/06/2011 14:03, miguel olivares varela wrote:
>
 > Hello
 >
 > i try to create a script to convert a date "YYYYmmddHHMMSS" as an 
UNIX timestamp. This is an example of my log file
 >
 > cat /tmp/test.log
 > 20110612112233
 > 20110126145535
 > 20110126185500
 >
 > here is my code:
 >
 > #! /usr/bin/python
 >
 > import os
 > import sys
 > import glob
 > import re
 > import time
 >
 > dir_log = "/tmp"
 > #loop to find files log in a directory
 > for log_files_in  in glob.glob(os.path.join(dir_log, '*.log') ):
 >         #print log_files
 >         file_brut = open(log_files_in, 'r')
 >         line_log = file_brut.readline()
 >         while line_log:
 >                 timestamp=int(time.mktime(time.strptime(line_log, 
"%Y%m%d%H%M%S")))
 >                 line_log=file_brut.readline()
 >                 print timestamp
 >
 >
 > And here the error code:
 >
 > Traceback (most recent call last):
 >   File "formatage_lms.py", line 32, in ?
 >     timestamp=int(time.mktime(time.strptime(line_cdr, "%Y%m%d%H%M%S")))
 >   File "/usr/lib64/python2.4/_strptime.py", line 296, in strptime
 >     raise ValueError("unconverted data remains: %s" %
 > ValueError: unconverted data remains:
 >
 >
 > I don't know what i'm diong wrong please could you help me
 >
I think the problem is that the line in "line_log" ends with "\n",
hence "unconverted data remains:" (at this point it prints the
unconverted data, which is the "\n").

Try this:

     timestamp=int(time.mktime(time.strptime(line_log.rstrip(), 
"%Y%m%d%H%M%S")))



More information about the Python-list mailing list