basic!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aboxylica
    New Member
    • Jul 2007
    • 111

    basic!

    i have a basic doubt.. i have a program that does a particular task..this prg(2) actually works with the o/p from another program(1). The program 1 gives the o/p in the form of a list that is written to a file.. now my program 2 works fine when i take its input as the o/p file but now i want to string in the prg2 to the prg1.
    the program one works on a list called masterList.this is written to a file and taken as i/p

    my program 2(which is working fine and independent)has these lines:
    Code:
    f=file("result.txt")# THIS CONTAINS STUFF FROM MASTERLIST FROM PRG1
    
    
    
    a=f.readlines()
    
    for i in a:
    # do a set of work
    now, when i string the 2 prg to the first i can just say:
    Code:
    for line in masterList:
    # do a set of work
    But this doesnt seem to work. but i dont see any difference or mistake
    do u see any??
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by aboxylica
    i have a basic doubt.. i have a program that does a particular task..this prg(2) actually works with the o/p from another program(1). The program 1 gives the o/p in the form of a list that is written to a file.. now my program 2 works fine when i take its input as the o/p file but now i want to string in the prg2 to the prg1.
    the program one works on a list called masterList.this is written to a file and taken as i/p

    my program 2(which is working fine and independent)has these lines:
    Code:
    f=file("result.txt")# THIS CONTAINS STUFF FROM MASTERLIST FROM PRG1
    
    
    
    a=f.readlines()
    
    for i in a:
    # do a set of work
    now, when i string the 2 prg to the first i can just say:
    Code:
    for line in masterList:
    # do a set of work
    But this doesnt seem to work. but i dont see any difference or mistake
    do u see any??
    Each line from the file will have a trailing newline character.

    Comment

    • aboxylica
      New Member
      • Jul 2007
      • 111

      #3
      Originally posted by bvdet
      Each line from the file will have a trailing newline character.
      you mean it will have a "\n" that will make the diffrence. so what should i do ? add a "\n" to my list? it doesnt really make sense to me!

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by aboxylica
        i have a basic doubt.. i have a program that does a particular task..this prg(2) actually works with the o/p from another program(1). The program 1 gives the o/p in the form of a list that is written to a file.. now my program 2 works fine when i take its input as the o/p file but now i want to string in the prg2 to the prg1.
        the program one works on a list called masterList.this is written to a file and taken as i/p

        my program 2(which is working fine and independent)has these lines:
        Code:
        f=file("result.txt")# THIS CONTAINS STUFF FROM MASTERLIST FROM PRG1
        
        
        
        a=f.readlines()
        
        for i in a:
        # do a set of work
        now, when i string the 2 prg to the first i can just say:
        Code:
        for line in masterList:
        # do a set of work
        But this doesnt seem to work. but i dont see any difference or mistake
        do u see any??
        I dont really understand what you mean. Do you want to add the code from program 2 to program 1 at the end or something else? Also, what is the error message you are getting?

        Comment

        • aboxylica
          New Member
          • Jul 2007
          • 111

          #5
          Originally posted by ilikepython
          I dont really understand what you mean. Do you want to add the code from program 2 to program 1 at the end or something else? Also, what is the error message you are getting?
          yes.. i want to add program 2 to program one.. i want to know what difference will
          readlines() from an o/p file(containing this masterList) and directly refering to for item in masterList make..
          The error can be corrected if i understand this difference

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by aboxylica
            you mean it will have a "\n" that will make the diffrence. so what should i do ? add a "\n" to my list? it doesnt really make sense to me!
            [code=Python]for item in masterList:
            ....do stuff....[/code][code=Python]lineList = [line.strip() for line in open("filename" ).readlines()]
            for item in lineList:
            ....do stuff....[/code]String method strip() will strip off leading and trailing whitespace characters. In the above examples, masterList and lineList will be equivalent.

            Comment

            Working...