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 6 Years Ago by Tops: n/a

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
    splitString.pop(0)
    
    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__':
    main()
def remove_first_column(text):
    splitString = text.split('--')

    splitString.pop(0)
    
    
    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__':
    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 6 Years Ago 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 6 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.