Reading and Writing

Discussion in 'Python' started by elias.goodman@gmail.com, Dec 23, 2004.

  1. Guest

    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.
     
    , Dec 23, 2004
    #1
    1. Advertisements

  2. Hi,

    wrote:
    > 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.


    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.
     
    Sir Galahad the chaste, Dec 23, 2004
    #2
    1. Advertisements

  3. Novitiate Guest

    -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
     
    Novitiate, Dec 23, 2004
    #3
  4. Novitiate Guest

    -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
     
    Novitiate, Dec 23, 2004
    #4
  5. Mike Meyer Guest

    Sir Galahad the chaste <> writes:

    > Hi,
    >
    > wrote:
    >> 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.

    >
    > 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()


    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()

    > lines.sort()
    > out = open("out.txt", "w")
    > for line in lines:
    > out.write(line)


    Those two should be
    out.writelines(lines)

    > out.close()


    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Dec 24, 2004
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TC
    Replies:
    3
    Views:
    26,758
    jessica
    May 19, 2004
  2. David Hearn
    Replies:
    1
    Views:
    392
    avnrao
    Jun 30, 2004
  3. =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=

    Reading and writing to a virtual directory

    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Oct 3, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    1,110
    Bruce Barker
    Oct 4, 2005
  4. =?Utf-8?B?UGF1bA==?=

    reading and writing to dataset

    =?Utf-8?B?UGF1bA==?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    551
    =?Utf-8?B?UGF1bA==?=
    Feb 6, 2006
  5. Stephen
    Replies:
    1
    Views:
    789
    Kevin Spencer
    Mar 16, 2006
  6. Mark Stijnman
    Replies:
    4
    Views:
    456
    Mark Stijnman
    May 12, 2005
  7. Replies:
    7
    Views:
    754
  8. Replies:
    3
    Views:
    286
    Bob Barrows [MVP]
    Jan 25, 2006
Loading...