Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7522 > unrolled thread
| Started by | MRAB <python@mrabarnett.plus.com> |
|---|---|
| First post | 2011-06-13 16:27 +0100 |
| Last post | 2011-06-13 16:27 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Date2Epoch script MRAB <python@mrabarnett.plus.com> - 2011-06-13 16:27 +0100
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2011-06-13 16:27 +0100 |
| Subject | Re: Date2Epoch script |
| Message-ID | <mailman.174.1307978880.11593.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web