Text file manipulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ritesh247
    New Member
    • Mar 2010
    • 12

    Text file manipulation

    I am using a text file in the form of 200 columns and 4 rows i.e

    4 5 6 7
    3 4 5 6
    an so on..

    i tried using:

    Code:
    openfile =open("variables.txt")
    for item in openfile:
        print item
    it prints the columns and the rows, but i don't know how to assign the values to my code.

    IS THERE ANY method of assigning each variable like lets say row 4 5 6 7 as A = 4, B=5, C =6 and D=7, use these variables in some manipulation and get an answer, and then on the second go it reads the next row and gets a result and like wise.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Yes. Save the data in a list of lists, something like this:
    Code:
    lineList = [line.strip().split() for line in fileObj]
    You can then access individual elements of the list using indexes:
    Code:
    lineList[row][column]

    Comment

    Working...