How to read fields from a file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kurda Yon

    #1

    How to read fields from a file?

    Hi,

    I have a file with data written in a "matrix" form. In other words,
    every row contains a fixed number of fields and I am interested in the
    value of each field of every row.

    I managed to read rows in the following way:
    f = open('filename. dat','r')
    for line in f:
    x = line
    f.close()

    However, I cannot find out how to split every line into fields? What
    should I do if I, let's say, need a value which is located in the
    third row in the second column (field).

    Thank you!
  • Kurda Yon

    #2
    Re: How to read fields from a file?

    I found line.split()! It works great! I think it is exactly what I
    need. Or may be there are some more elegant solutions?

    Comment

    • =?ISO-8859-13?Q?Kristaps_K=FBlis?=

      #3
      Re: How to read fields from a file?

      On Oct 1, 2:58 am, Kurda Yon <kurda...@yahoo .comwrote:
      I found line.split()! It works great! I think it is exactly what I
      need. Or may be there are some more elegant solutions
      Why are you posting in comp.lang.php questions about language with is
      not connected to PHP ?

      LOL

      Comment

      • Kurda Yon

        #4
        Re: How to read fields from a file?

        On Oct 1, 5:07 am, Kristaps Kûlis <Kristaps.Ku... @gmail.comwrote :
        On Oct 1, 2:58 am, Kurda Yon <kurda...@yahoo .comwrote:
        >
        I found line.split()! It works great! I think it is exactly what I
        need. Or may be there are some more elegant solutions
        >
        Why are you posting in comp.lang.php questions about language with is
        not connected to PHP ?
        >
        LOL
        Sorry, I did it by a mistake. It should be python... Thank you that
        you indicated that.

        Comment

        Working...