How to extract data from a txt file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbphydb
    New Member
    • Apr 2010
    • 22

    #1

    How to extract data from a txt file

    Hi

    My txt file looks something like this:
    Branch: ltm_7.4
    Destination: Test 5

    Code:
    lines = open("branch_dest.txt").readlines()
    lines=[x.split() for x in lines]
    print lines
    
    branch = "%s" % lines[0][1].strip(': ')
    print branch
    destination = "%s" % lines[1][2].strip(': ')
    print destination
    I need to extract the branch name and the destination. But i get the below output.

    [['Branch:', 'ltm_7.4'], ['Destination:', 'Test', '5']]
    ltm_7.4
    5

    Branch is ok. But the destination needs to be Test 5. Kindly help as i am new to Python.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Split the string on the colon character - s.split(":")
    "Test 5" will then be intact.

    Comment

    Working...