|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
|
#3
|
||||
|
||||
|
Re: reply
Quote:
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 Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month |
|
#4
|
|||
|
|||
|
reply
Thank you Scorpions4ever. You are always a big help.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > split and splitfields function cause an error when the program run |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|