Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7522
| Date | 2011-06-13 16:27 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: Date2Epoch script |
| References | <COL120-W64F6AB042603E36D1389A0DC690@phx.gbl> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.174.1307978880.11593.python-list@python.org> (permalink) |
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")))
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Date2Epoch script MRAB <python@mrabarnett.plus.com> - 2011-06-13 16:27 +0100
csiph-web