Read csv file and create a new file

Dave Angel davea at davea.name
Thu Feb 28 14:35:37 EST 2013


On 02/28/2013 02:14 PM, io wrote:
> 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
>
>


Start by making a set out of the second file.  Don't forget to remove 
whitespace with strip()

Then loop through the first file.  For each line, split() it by 
whitespace, and conditionally write the line to the third file.  The 
test would be something like:
      if  fields[0] in myset:
            outfile.write(line)


-- 
DaveA



More information about the Python-list mailing list