Re: Read csv file and create a new file

Discussion in 'Python' started by Dennis Lee Bieber, Mar 1, 2013.

  1. On 28 Feb 2013 19:14:36 GMT, io <> declaimed the
    following in gmane.comp.python.general:

    > Hi,
    >
    > i have to files.
    >
    > First file is a csv file
    > Second file is a plain text file where each row has a value (text)
    >
    > I want to be able to create a third file using data from the first file
    > excluding the values listed in the second file.
    >
    > Example:
    >
    > First file:
    > -----------
    >
    > mtgoxeur 12 24 36
    > mtgoxusd 10 12 14
    > mtgoxpln 2 4 6
    >
    >
    > Second file:
    > ------------
    >
    > mtgoxusd
    >
    >
    >
    > Third File (the resulting one) :
    > --------------------------------
    >
    > mtgoxeur 12 24 36
    > mtgoxpln 2 4 6
    >
    >
    >
    > Thanks to anyone that can help
    >


    This appears to be a variation of standard sort/merge operation. The
    variation being that you /do not/ write lines of one file when they
    match the other file.

    While an in-memory sort may be feasible (unless the data file is
    extremely large) I would probably invoke your OS sort module on each
    file...

    Then (pseudo-code, not executable python)

    data = csvin.next()
    key = keyfile.readln().strip()

    while data and key:
    if data[0] < key:
    csvout.write(data)
    data = datafile.readln().strip()
    elif data[0] = key:
    data =csvin.next()
    #don't increment key to catch duplicates in data
    elif data[0] > key:
    key = keyfile.readln().strip()

    while data:
    #no more keys to remove, copy rest of data file
    csvout.write(data)
    data = csvin.next()
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Mar 1, 2013
    #1
    1. Advertising

  2. On Mon, 04 Mar 2013 19:04:14 +0000, io wrote:

    > What you wrote seems interesting but i haven't understood. Can you
    > explain in simple words considering i'm italian and i'm not
    > understanding so well some terms you use. Sorry, i'm sure you are
    > suggesting something really valid but can't understand it.
    >
    > Marco.


    Who are you talking to? Your reply has no context.

    When replying to a message, please quote enough of the previous message
    so that readers can understand what you are replying to. There is usually
    no need to quote the entire message. Often just a few lines is enough.

    If you spend some time reading other people's questions and answers, you
    will see what I mean.



    --
    Steven
    Steven D'Aprano, Mar 4, 2013
    #2
    1. Advertising

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. jliu66
    Replies:
    0
    Views:
    506
    jliu66
    Oct 19, 2007
  2. Li Chen
    Replies:
    18
    Views:
    659
    Azmi Farih
    Mar 23, 2010
  3. Neil Cerutti

    Re: Read csv file and create a new file

    Neil Cerutti, Feb 28, 2013, in forum: Python
    Replies:
    0
    Views:
    156
    Neil Cerutti
    Feb 28, 2013
  4. Dave Angel
    Replies:
    0
    Views:
    154
    Dave Angel
    Feb 28, 2013
  5. Neil Cerutti

    Re: Read csv file and create a new file

    Neil Cerutti, Feb 28, 2013, in forum: Python
    Replies:
    2
    Views:
    149
    Dave Angel
    Mar 1, 2013
Loading...

Share This Page