Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing timestamps in a log file 1

Status
Not open for further replies.

blacknred

Technical User
Joined
Nov 24, 2010
Messages
12
Location
IN
I'm trying to parse xml file and convert all the unixtimestamps in it to datetime...

<Sample> <TimeStamp>1291052077</TimeStamp> blah blah blah blah </Sample> <Sample> <TimeStamp>1291052077</TimeStamp> blah blah blah blah </Sample> </Content>


import fileinput

file = raw_input("Enter file: ")
file = open(file, "wb+")

for line in fileinput.FileInput(file,inplace=1):
if "TimeStamp" in line:
line=line.replace(<old>,<new>) // don't know how to get all instances of the time
pass

print(datetime.datetime.fromtimestamp(int("1284101485")).strftime('%Y-%m-%d %H:%M:%S'))

I've got some clues as above, but struggling to piece together things. Any thoughts?
 
Hi

I would skip the XML parsing and would simply treat it like text :
Code:
[blue]>>>[/blue] str="""<Sample>
[blue]...[/blue]                         <TimeStamp>1291052077</TimeStamp>
[blue]...[/blue]                         blah blah
[blue]...[/blue]                         blah blah
[blue]...[/blue]                 </Sample>
[blue]...[/blue]         
[blue]...[/blue]                 <Sample>
[blue]...[/blue]                         <TimeStamp>1291052077</TimeStamp>
[blue]...[/blue]                         blah blah
[blue]...[/blue]                         blah blah
[blue]...[/blue]                 </Sample>
[blue]...[/blue]         </Content>"""
[blue]>>>[/blue] def makeitdate(m):
[blue]...[/blue]   return m.group(1)+datetime.datetime.fromtimestamp(int(m.group(2))).strftime('%Y-%m-%d %H:%M:%S')
[blue]...[/blue] 
[blue]>>>[/blue] print re.sub(r'(<TimeStamp>)(\d+)',makeitdate,str)
<Sample>
                        <TimeStamp>2010-11-29 19:34:37</TimeStamp>
                        blah blah
                        blah blah
                </Sample>
        
                <Sample>
                        <TimeStamp>2010-11-29 19:34:37</TimeStamp>
                        blah blah
                        blah blah
                </Sample>
        </Content>

Feherke.
 
That helps, but I wish to do it on a file as opposed to a string ....
 
Hi

I would change it like this :
Code:
[red]import[/red] fileinput
[red]import[/red] re
[red]import[/red] datetime

[b]def[/b] [COLOR=darkgoldenrod]makeitdate[/color][teal]([/teal]m[teal]):[/teal]
    [b]return[/b] m[teal].[/teal][COLOR=darkgoldenrod]group[/color][teal]([/teal][purple]1[/purple][teal])+[/teal]datetime[teal].[/teal]datetime[teal].[/teal][COLOR=darkgoldenrod]fromtimestamp[/color][teal]([/teal][COLOR=darkgoldenrod]int[/color][teal]([/teal]m[teal].[/teal][COLOR=darkgoldenrod]group[/color][teal]([/teal][purple]2[/purple][teal]))).[/teal][COLOR=darkgoldenrod]strftime[/color][teal]([/teal][green][i]'%Y-%m-%d %H:%M:%S'[/i][/green][teal])[/teal]

[b]for[/b] line [b]in[/b] fileinput[teal].[/teal][COLOR=darkgoldenrod]input[/color][teal]([/teal]inplace[teal]=[/teal]True[teal]):[/teal]
    [b]print[/b] re[teal].[/teal][COLOR=darkgoldenrod]sub[/color][teal]([/teal]r[green][i]'(<TimeStamp>)(\d+)'[/i][/green][teal],[/teal]makeitdate[teal],[/teal]line[teal].[/teal][COLOR=darkgoldenrod]rstrip[/color][teal]())[/teal]
Then run it as :
Code:
python makeitdate.py /path/to/file.xml

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top