Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Read csv file and create a new file

Reply
Thread Tools

Re: Read csv file and create a new file

 
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      03-01-2013
On 28 Feb 2013 19:14:36 GMT, io <(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-04-2013
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Read csv file and create a new file Neil Cerutti Python 2 03-01-2013 12:13 AM
Re: Read csv file and create a new file Dave Angel Python 0 02-28-2013 07:35 PM
Re: Read csv file and create a new file Neil Cerutti Python 0 02-28-2013 07:32 PM
read CSV file using csv library Li Chen Ruby 18 03-23-2010 12:44 AM
read and write csv file using csv module jliu66 Python 0 10-19-2007 03:12 PM



Advertisments