Q:

how to split a line for each space and get each word in python

i got this error when i tried using this file as routable.txt as input
C  10.0.0.0  FastEthernet0/0
C  12.0.0.0  Serial0/1/0
R  13.0.0.0  12.0.0.2  Serial0/1/0
R  14.0.0.0  12.0.0.2  Serial0/1/0
R  15.0.0.0  12.0.0.2  Serial0/1/0
R  20.0.0.0  12.0.0.2  Serial0/1/0
R  25.0.0.0  12.0.0.2  Serial0/1/0
data=open('routable.txt')
data.seek(0)
for each_line in data:
    print (each_line,end=' ')
    (c_r, dip, via, eth)=each_line.split("  ", maxsplit=4)

data.close()

error is 
C  10.0.0.0  FastEthernet0/0
Traceback (most recent call last):
  File "C:/Python34/acnrouting.py", line 5, in 
    (c_r, dip, via, eth)=each_line.split("  ", maxsplit=4)
ValueError: need more than 3 values to unpack

A:

with open('routable.txt') as data:
    for line in data:
        if not line:
            continue # skip empty lines
        print(line, end="")
        parts = line.split()
        if len(parts) == 4:
            c_r, dip, via, eth = parts
        elif len(parts) == 3:
            c_r, dip, eth = parts
        else:
            print("Bad Line!")

Notes:

The with construct is the preferred python way of handling open files because it assures that the file handle will get closed when not in use.
data.seek(0) is unnecessary.  When a file is opened, the file pointer is always at the start of the file.
The sample input, as it appeared on stackoverflow, had blank (empty) lines.  An if statement was added to assure that these lines are ignored.
Not all of your input lines had four elements.  Some had three.  This code tests for how many elements there were and unpacks appropriately.

