US --9--15--13--37
GE --10 --13--7--30
CA-- 14 -- 7 -- 5-- 26

(those dashes are meant to be spaces)

How do i remove the first column of the text above so the output would look something like this?

9--15 --13 --37
10 --13--7--30
14 --7-- 5--26

Edited by Tops: n/a

6 Years
Discussion Span
Last Post by pyTony
from string import split

def main():
    testString = "US 9 15 13 37"
    #using split from the string library
    #splitting it based on whitespace
    splitString = testString.split(' ')
    #now splitString is a list and we remove the first node
    print splitString   #prints as a list
    #Now just concatenate the strings together and add whitespace between
    finalString = ""
    for i in splitString:
        finalString += i+" "
    print finalString

if __name__ == '__main__':
def remove_first_column(text):
    splitString = text.split('--')

    finalString = " -- ".join(
        [item.strip() for item in splitString])
    return finalString

def main():
    testString = """US --9--15--13--37
GE --10 --13--7--30
CA-- 14 -- 7 -- 5-- 26"""
    for line in testString.split("\n"):
        print(remove_first_column(line))  #py2.5  print remove_first_column(line)

if __name__ == '__main__':

Just improving on the above code.
It's Python 3.0 code, if you want 2.5, then look at the comment on line 17.

Edited by jcao219: n/a


You don't need to split and join...

datas = """US 9 15 13 37
GE 10 13 7 30
CA 14 7 5 26""".split("\n")
for l in datas:
    print l.split(None,1)[1] #  the 1 split argument will tell to split only once

# Faster
# you can also use lists comprehensions :
print [l.split(None,1)[1] for l in datas]
# or
print "\n".join([l.split(None,1)[1] for l in datas])

Interesting way, I had not noticed the second parameter of split before.

To my mind came however another method which I find intuitive using the partition method with space as separation, I do not know if it is slow or fast, but looks similar to method of split with 1 as second parameter: (if you prefer to loose few bytes put a,b,c instead of c,c,c which reuses the a varuable (little hack ;))

datas = ["US --9--15--13--37", "GE --10 --13--7--30", "CA--14--7--5--26"]

for l in datas:
    (a,a,a)=l.partition("--") ## need only last value of partition
    print a

Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.