[CentralOH] parsing Windows command line output
Mark Peters
mpeters42 at gmail.com
Wed Jul 23 15:30:27 CEST 2008
Use the default option to split:
*split*( [sep [,maxsplit]]) Return a list of the words in the string, using
sep as the delimiter string. If maxsplit is given, at most maxsplit splits
are done. (thus, the list will have at most maxsplit+1 elements). If
maxsplit is not specified, then there is no limit on the number of splits
(all possible splits are made). Consecutive delimiters are not grouped
together and are deemed to delimit empty strings (for example, "
'1„2'.split(',')"returns "['1', '', '2']"). The sep argument may consist of
multiple characters (for example, "'1, 2, 3'.split(', ')" returns "['1',
'2', '3']"). Splitting an empty string with a specified separator returns "
['']".
If sep is not specified or is None, a different splitting algorithm is
applied. First, whitespace characters (spaces, tabs, newlines, returns, and
formfeeds) are stripped from both ends. Then, words are separated by
arbitrary length strings of whitespace characters. Consecutive whitespace
delimiters are treated as a single delimiter ("'1 2 3'.split()" returns "['1',
'2', '3']"). Splitting an empty string or a string consisting of just
whitespace returns an empty list.
By default, it splits using *any consecutive* whitespace.
Alternatively, you could use a regex.
Mark Peters
On Wed, Jul 23, 2008 at 9:25 AM, Eric Lake <ericlake at gmail.com> wrote:
> Turns out this may not work because Microsoft does not always separate
> the columns of data with 2 or more spaces. So it looks like I'm still
> stumped on this one.
>
> On Wed, Jul 23, 2008 at 8:44 AM, Eric Lake <ericlake at gmail.com> wrote:
> > I may have answered my own question. I know that this could probably
> > be done in a more elegant way so I am looking for suggestions. Here is
> > the code that I have come up with so far:
> >
> > data = open('file.txt', 'r')
> >
> > data.next()
> > data.next()
> > data.next()
> > data.next()
> >
> > for line in data:
> > clusdata = []
> > line.strip()
> > info = line.split(' ')
> > for x in info:
> > if x != '':
> > x = x.strip()
> > if x .endswith('\\r\\n'):
> > x = x.strip('\\r\\n')
> > clusdata.append(x)
> > print clusdata
> >
> > data.close()
> >
> >
> >
> > On Wed, Jul 23, 2008 at 7:52 AM, Eric Lake <ericlake at gmail.com> wrote:
> >> I have been looking for a way to monitor a Windows cluster that we
> >> have at work. I was really hoping for a good wmi class that I could
> >> use from my XP machine to do it but I have not been lucky finding one.
> >> I did however come across the 'cluster' command. I am thinking that I
> >> could just call that from within my python code and parse the output
> >> to get what I need. I want to get all of the data from the output and
> >> then put it in a database. I can not see how to split the lines up
> >> though. It would be easy if it was comma delimited but it seems to use
> >> a varying number of spaces. My only thought is that I could do a split
> >> on anything that is more than 2 spaces.
> >>
> >> The output of the command is in the attached file.
> >>
> >> --
> >> Thanks,
> >>
> >> Eric Lake
> >>
> >
> >
> >
> > --
> > Thanks,
> >
> > Eric Lake
> >
>
>
>
> --
> Thanks,
>
> Eric Lake
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> http://mail.python.org/mailman/listinfo/centraloh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20080723/57a6ccaf/attachment.htm>
More information about the CentralOH
mailing list