Reading from input file.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paczkow@gmail.com

    #1

    Reading from input file.

    Dear Python Community,

    I am an engineering and I am experiencing some trouble. Having output
    data from other software I want to use it. To achieve this I decided to
    use Python since this language is the best known for me.

    So.. for my future Python script, the input data are in form of:
    1
    1233.2E-3 2123.2323 2E+2 3453.3E+1
    1233.2E-3 2123.2323 2E+2 3453.3E+1
    1233.2E-3 2123.2323 2E+2 3453.3E+1
    2
    1233.2E-3 2123.2323 2E+2 3453.3E+1
    1233.2E-3 2123.2323 2E+2 3453.3E+1
    1233.2E-3 2123.2323 2E+2 3453.3E+1
    and so on...

    The number 1 and 2 and so on, are the numbers of measurments, which are
    different in number depending on analyzed case. So sometimes it could
    be 3 and sometimes 1000 or more.

    What I want to achieve, I want to compare or rather get difference
    between two similar input files. I tried to work with "readline"
    command, however it is hard to use it (for me) since input files are
    different in its size (3,1000 or more input points). Therefore, I
    decided to ask you for help how to do it. Esspecially how to make that
    python script which will be reading data, each line, and each column
    of input file and therefore will compare it with different input file
    giving finally difference size between each measures for example:
    First data at point 1 minus first data from point 1 from different
    input file:
    (1233.2E-3) - (1233.3E-3) = -0.1E-3

    Looking forward for you answer,

    Best regards,

    Krystian

  • Sheldon

    #2
    Re: Reading from input file.

    Hi Kyrstian,

    Try reading the file with ope(file,'r').r ead()
    and then split the string using '\n' as the delimiter (this will break
    the file down into each individual line consisting of several columns).
    Then string split each line of the file (unsing ' ' as the delimiter),
    they should now be 3, or more, columns per line, and store each column
    in a vector - adding each line to the vector with append function. Then
    place each vector in a list.
    Now you should have a file to compare with the next one. But watch out
    for files of varying lengths as they cannot be compared on a one-to-one
    basis. One you have each file in a list or vector or dictionary or
    array style, comparison and calculations should be straight forward.

    Hope this points you in the right direction. I haven't tried this
    myself but I have done a similar thing earlier but I strongly believe
    that it should work.

    Sincerely,
    Sheldon

    Comment

    • paczkow@gmail.com

      #3
      Re: Reading from input file.

      I do not clearly understand what you say.

      I am no going to change input files in anyway. I just want to read them
      with Python, and postprocess.

      I understand:
      open(inputfile. txt, 'r').read()
      However what you mean saying split the string using '\n'. Where should
      I put it? After file name, or what? Sorry, but I am still learing
      Python.

      Looking forward hearing from you,

      Regards

      Comment

      • paczkow@gmail.com

        #4
        Re: Reading from input file.

        I do not clearly understand what you say.

        I am no going to change input files in anyway. I just want to read them
        with Python, and postprocess.

        I understand:
        open(inputfile. txt, 'r').read()
        However what you mean saying split the string using '\n'. Where should
        I put it? After file name, or what? Sorry, but I am still learing
        Python.

        Looking forward hearing from you,

        Regards

        Comment

        • David Murmann

          #5
          Re: Reading from input file.

          hi!

          i find it rather hard to understand your problem, but i'll try anyway:

          paczkow@gmail.c om schrieb:[color=blue]
          > So.. for my future Python script, the input data are in form of:
          > 1
          > 1233.2E-3 2123.2323 2E+2 3453.3E+1
          > 1233.2E-3 2123.2323 2E+2 3453.3E+1
          > 1233.2E-3 2123.2323 2E+2 3453.3E+1
          > 2
          > 1233.2E-3 2123.2323 2E+2 3453.3E+1
          > 1233.2E-3 2123.2323 2E+2 3453.3E+1
          > 1233.2E-3 2123.2323 2E+2 3453.3E+1
          > and so on...[/color]

          are these 1's and 2's there to indicate different files, or do
          they really appear in the input files?
          [color=blue]
          > The number 1 and 2 and so on, are the numbers of measurments, which are
          > different in number depending on analyzed case. So sometimes it could
          > be 3 and sometimes 1000 or more.
          >
          > What I want to achieve, I want to compare or rather get difference
          > between two similar input files. I tried to work with "readline"
          > command, however it is hard to use it (for me) since input files are
          > different in its size (3,1000 or more input points). Therefore, I
          > decided to ask you for help how to do it. Esspecially how to make that
          > python script which will be reading data, each line, and each column
          > of input file and therefore will compare it with different input file
          > giving finally difference size between each measures for example:
          > First data at point 1 minus first data from point 1 from different
          > input file:
          > (1233.2E-3) - (1233.3E-3) = -0.1E-3[/color]

          well, from what i understood you might try something like this:

          -------------------------------
          from itertools import izip

          # iterate over two files at once (stopping at the shorter ones end)
          for lines in izip(file('inpu tfile1'), file('inputfile 2')):

          # split the lines into columns
          columns = [line.split() for line in lines]

          # calculate and print the difference of the individual entries
          for x, y in izip(*columns):
          print float(x)-float(y),
          print
          -------------------------------

          if this (or something similar) does not work for you, you could take
          a look at the difflib module:

          Source code: Lib/difflib.py This module provides classes and functions for comparing sequences. Most of them compare sequences of text lines (for example lists of strings, or file objects) and prod...


          hope that helps, David.

          Comment

          • Sheldon

            #6
            Re: Reading from input file.

            Hi,

            after you have read the file then split it like this:
            file = open('inputfile .txt', 'r').read()
            import string
            file = string.split(fi le,'\n')
            now if you print file[0] you should only get the first line.
            Be careful and examine the file for non-continuous sections where a
            line is empty. That is to say where file[x] = ' '. You should remove
            these lines from the txt files before you read it with python. It maybe
            so that python reads the entire file, empty spaces and all, and then
            you have to remove them after the split with a WHERE statement. This is
            a much easier way so lets hope that this is so.

            I hope this was more clear and helpful this time around.

            Cheers
            Sheldon

            Comment

            • Mike Meyer

              #7
              Re: Reading from input file.

              "Sheldon" <shejo284@gmail .com> writes:[color=blue]
              > after you have read the file then split it like this:
              > file = open('inputfile .txt', 'r').read()
              > import string
              > file = string.split(fi le,'\n')[/color]

              You're doing things the hard way - at least if you have a modern
              Python. The above can be done as:

              file = open('inputfile .txt', 'r').read().spl it('\n')

              Or even better

              file = open('inputfile .txt', 'r').readlines( )

              will all give you the same result.

              BTW, it's a bad habit to leave open files laying around. It's also a
              bad habit to use the name of builtins (like "file") as variable names.
              [color=blue]
              > now if you print file[0] you should only get the first line.
              > Be careful and examine the file for non-continuous sections where a
              > line is empty. That is to say where file[x] = ' '. You should remove
              > these lines from the txt files before you read it with python. It maybe
              > so that python reads the entire file, empty spaces and all, and then
              > you have to remove them after the split with a WHERE statement. This is
              > a much easier way so lets hope that this is so.[/color]

              Huh? What in the OP makes you think this is even necesary?

              <mike
              --
              Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
              Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

              Comment

              • Sheldon

                #8
                Re: Reading from input file.

                So Mike if you can do better then do it then! There are many ways do
                solve a problem, perhaps you have not learned that yet. At first this
                guy didn't know what to do, so he had to begin somewhere. Now you can
                take him much further, I am sure but the journey might not be so
                pleasant. Your attitude really needs major adjustment but then again, I
                have to consider the source.
                FYI, the file[0] part was simply to check if it worked.

                Don't waste your time with me and my "hard way", it is Krystian you
                should be helping!

                Comment

                • Mike Meyer

                  #9
                  Re: Reading from input file.

                  "Sheldon" <shejo284@gmail .com> writes:[color=blue]
                  > So Mike if you can do better then do it then! There are many ways do
                  > solve a problem, perhaps you have not learned that yet. At first this
                  > guy didn't know what to do, so he had to begin somewhere. Now you can
                  > take him much further, I am sure but the journey might not be so
                  > pleasant. Your attitude really needs major adjustment but then again, I
                  > have to consider the source.[/color]

                  Let's double check: I didn't insult you. I didn't insult the original
                  poster. I didn't say the code was bad, or ugly, or otherwise
                  intolerable. All I did was point out that - with a modern Python -
                  there's a shorter, easier to read way to write it. Pretty typical
                  c.l.python behavior: demonstrating how posted code can be improved in
                  some way. Personally, I encourage this behavior, because it helps
                  everybody write better code.

                  In return, I get insulted, told I need an attitude adjustment, and
                  insulted a second time.

                  If seeing people improve your code ticks you off so badly, you should
                  think *very* hard before you post it to c.l.python. Writing code that
                  can't be improved is nearly impossible, so it's very likely that any
                  code posted here - by anyone - someone will know how to improve.

                  <mike
                  --
                  Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                  Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                  Comment

                  • Sheldon

                    #10
                    Re: Reading from input file.

                    Sorry Mike, after seeing so many "experts" beat up others for not being
                    as "smart" as they are, I intrepreted your words incorrectly - my
                    apologies. I am not in the least bit against impproving my programming.
                    I liked what you did and thanks for the pointers.

                    Sheldon

                    Comment

                    Working...