Reading and Writing

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

    #1

    Reading and Writing

    I am new to Python and programming. I am looking for some basic
    instruction here.

    How should I: Open a Text file, read from it, modify it, print to
    another .txt?

    For instance: Read a string, sort it, write the sorted string.

    I understand the algorithms but I don't know the actual mechanics of
    Python.

    Any help or advice as to where to look would be great.

    Thanks.

  • Sir Galahad the chaste

    #2
    Re: Reading and Writing

    Hi,

    elias.goodman@g mail.com wrote:[color=blue]
    > How should I: Open a Text file, read from it, modify it, print to
    > another .txt?
    >
    > For instance: Read a string, sort it, write the sorted string.[/color]

    What do you mean by "sorting"? If you want to sort the lines contained
    in a file, you could do something like this.

    $ cat in.txt
    foo
    bar
    baz
    ham
    spam
    $ cat process.py
    #!/usr/bin/env python
    lines = open("in.txt"). readlines()
    lines.sort()
    out = open("out.txt", "w")
    for line in lines:
    out.write(line)
    out.close()
    $ python process.py
    $ cat out.txt
    bar
    baz
    foo
    ham
    spam

    Regards
    G.

    Comment

    • Novitiate

      #3
      Re: Reading and Writing

      -Galahad,

      Thank you very much. I will give it a shot and see if I can make it
      hapen. I think this will help a lot.

      I was just trying to implement a simple sorting algorithm that I knew
      from C++, for practice but I couldn't figure the mechanics of Python.
      Thanks again,

      Novitiate

      Comment

      • Novitiate

        #4
        Re: Reading and Writing

        -Galahad,

        Thank you very much. I will give it a shot and see if I can make it
        hapen. I think this will help a lot.

        I was just trying to implement a simple sorting algorithm that I knew
        from C++, for practice but I couldn't figure the mechanics of Python.
        Thanks again,

        Novitiate

        Comment

        • Mike Meyer

          #5
          Re: Reading and Writing

          Sir Galahad the chaste <sttmjoc0@fastm ail.fm> writes:
          [color=blue]
          > Hi,
          >
          > elias.goodman@g mail.com wrote:[color=green]
          >> How should I: Open a Text file, read from it, modify it, print to
          >> another .txt?
          >> For instance: Read a string, sort it, write the sorted string.[/color]
          >
          > What do you mean by "sorting"? If you want to sort the lines contained
          > in a file, you could do something like this.
          >
          > $ cat in.txt
          > foo
          > bar
          > baz
          > ham
          > spam
          > $ cat process.py
          > #!/usr/bin/env python
          > lines = open("in.txt"). readlines()[/color]

          Some people consider it bad style to leave opened files lieing around,
          so this shold be:

          f = open("in.txt")
          lines = f.readlines()
          f.close()
          [color=blue]
          > lines.sort()
          > out = open("out.txt", "w")
          > for line in lines:
          > out.write(line)[/color]

          Those two should be
          out.writelines( lines)
          [color=blue]
          > out.close()[/color]

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

          Comment

          Working...