compound strip() string problem

D

Dylan Wilson

Hi,
I'm new to python and i have a string problem.
My problem is this
--------------------------------------------------------------------------
>>>import time
>>>time = time.asctime()
>>>time 'Fri Apr 08 22:14:14 2005'
>>>ti = time[0:13]
>>>me = time[14:16]
>>>time = ti + me
>>>time
'Fri Apr 08 2214'
--------------------------------------------------------------------------
Now i need to compond that string remove the whitespace if you will.Well
i read up on strip(), lstrip() and rstrip() and all i could deduce was
that they striped the whitespace from the start and/or end of the
string.But I tried that anyway and failed.Is there an easier way than i
did it below? I'm sorry it's ugly and tedious.
--------------------------------------------------------------------------
#!/bin/bash/env python

import os, time

#Figure out what os this is
platform = os.name

#Create string with date, time and extension for our pcap file
ext = '.out'
time = time.asctime()
ti = time[0:13] #
me = time[14:16] #
time = ti + me #There has to be a better way to do this?
fo = time[0:3] #
rm = time[4:7] #
at = time[11:18]
time = fo + rm + at + ext

#Get rid of yukkies
del ti, me, ext, fo, rm, at

#create command string
flag = '-w'
wincommand = 'c:/progra~1/ethereal/tethereal'
lincommand = '/usr/sbin/./tethereal'

#run tethereal and send the output to a pcap file DDDMMMHHMM.out

if platform == 'nt':
os.system('%s %s %s' % (wincommand, flag, time))

if platform == 'posix':
os.system('%s %s %s' % (lincommand, flag, time))
 
S

Sidharth Kuruvila

The time module has a function called 'strftime' which can retyrn the
time in the the format you want to. So you really don't need to parse
the string returned by asctime the way you are doing.


Hi,
I'm new to python and i have a string problem.
My problem is this
--------------------------------------------------------------------------
import time
time = time.asctime()
time 'Fri Apr 08 22:14:14 2005'
ti = time[0:13]
me = time[14:16]
time = ti + me
time
'Fri Apr 08 2214'
--------------------------------------------------------------------------
Now i need to compond that string remove the whitespace if you will.Well
i read up on strip(), lstrip() and rstrip() and all i could deduce was
that they striped the whitespace from the start and/or end of the
string.But I tried that anyway and failed.Is there an easier way than i
did it below? I'm sorry it's ugly and tedious.
--------------------------------------------------------------------------
#!/bin/bash/env python

import os, time

#Figure out what os this is
platform = os.name

#Create string with date, time and extension for our pcap file
ext = '.out'
time = time.asctime()
ti = time[0:13] #
me = time[14:16] #
time = ti + me #There has to be a better way to do this?
fo = time[0:3] #
rm = time[4:7] #
at = time[11:18]
time = fo + rm + at + ext

#Get rid of yukkies
del ti, me, ext, fo, rm, at

#create command string
flag = '-w'
wincommand = 'c:/progra~1/ethereal/tethereal'
lincommand = '/usr/sbin/./tethereal'

#run tethereal and send the output to a pcap file DDDMMMHHMM.out

if platform == 'nt':
os.system('%s %s %s' % (wincommand, flag, time))

if platform == 'posix':
os.system('%s %s %s' % (lincommand, flag, time))
 
J

Jeffrey Froman

Dylan said:
Now i need to compond that string remove the whitespace if you will.Well
i read up on strip(), lstrip() and rstrip() and all i could deduce was
that they striped the whitespace from the start and/or end of the
string.But I tried that anyway and failed.Is there an easier way than i
did it below?

As Sidharth Kuruvila pointed out, time.strftime() is probably the best
choice for your situation. For the general case of removing whitespace from
a sting, one method is:

''.join(mystring.split())


Jeffrey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top