#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    split and splitfields function cause an error when the program run


    split and splitfields function cause an error when the program run

    =============================
    Traceback (most recent call last):
    File "./read-ifconfig_1.py", line 15, in ?
    split(eth0,":",0)
    NameError: name 'split' is not defined


    ==============================
    #!/usr/bin/python

    import os, sys, os.path, operator, string

    f = os.popen3("ifconfig")
    if_config_list = string.strip ( f[1].read() ).split(" ")

    eth0 = if_config_list[23]
    eth1 = if_config_list[140]
    print "\neth0 = ", eth0
    print "\neth1 = ", eth1

    #eth0 now has a format of addr:216.143.22.145

    #I want to get rid of the word "addr:" from the value eth0
    eth0_split = split(eth0,":",0)
    print "\neth0_split = ", eth0_split
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    reply


    The statement below will solve the problem. As for the original problem, the function
    split(s[, sep[, maxsplit]]) and
    splitfields(s[, sep[, maxsplit]]) are defined in the Python manual I am not sure why it does not work.
    ======================================

    for i in eth0:
    eth0_split = eth0.split(":",1)

    print "The colon split at position number. i = ", i
    print "eth0_split = ", eth0_split
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,615
    Rep Power
    4247

    Re: reply


    Originally posted by linh
    The statement below will solve the problem. As for the original problem, the function
    split(s[, sep[, maxsplit]]) and
    splitfields(s[, sep[, maxsplit]]) are defined in the Python manual I am not sure why it does not work.
    That's because split and splitfields are in the string module. Either import everything from the string module like this:
    from string import *
    foo = split(somestring, separator)

    or qualify the namespace:
    import string
    foo = string.split(somestring, separator)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    reply


    Thank you Scorpions4ever. You are always a big help.

IMN logo majestic logo threadwatch logo seochat tools logo