18

I have abx.csv file having three columns. I would like to filter the data which is having Application as Central and write it in same .csv file

User ID   Name        Application  

001       Ajohns      ABI
002       Fjerry      Central
900       Xknight     RFC
300       JollK       QDI
078       Demik       Central

I need to write User ID,Name,Apllication in three column in same .csv file (modifying the existing file)

San4ez
  • 8,091
  • 4
  • 41
  • 62
user1386541
  • 181
  • 1
  • 1
  • 4

2 Answers2

20
import csv
reader = csv.reader(open(r"abx.csv"),delimiter=' ')
filtered = filter(lambda p: 'Central' == p[2], reader)
csv.writer(open(r"abx.csv",'w'),delimiter=' ').writerows(filtered)
mshsayem
  • 17,557
  • 11
  • 61
  • 69
16

You should use different output filename. Even if you want the name to be the same, you should use some temporary name and finally rename file. Otherwise you have to read file to memory at first

import csv
with open('infile','r') as fin, open ('outfile','w') as fout:
    writer = csv.writer(fout, delimiter=' ')            
    for row in csv.reader(fin, delimiter=' '):
        if row[2] == 'Central':
             writer.writerow(row)
Frantisek Kossuth
  • 3,524
  • 2
  • 23
  • 42
San4ez
  • 8,091
  • 4
  • 41
  • 62
  • 4
    You don't need to nest those `with` context managers on separate indent levels. Instead, do `with open('infile','r), open ('outfile','w') as fin, fout:`. – Li-aung Yip May 10 '12 at 08:59
  • Actually, also note that you're going to need to specify some dialect parameters here. `delimiter=" "` at a minimum. – Li-aung Yip May 10 '12 at 09:15
  • I thought OP could do something by himself – San4ez May 10 '12 at 09:19
  • 3
    If he copies and pastes your code, it won't work, and he'll have little idea why. You could at least include a disclaimer that he's going to need to adjust for space-separated files - a pointer to the `csv` module documentation wouldn't hurt. As a bonus, this introduces the OP to the habit of reading the documentation - win-win! – Li-aung Yip May 10 '12 at 09:21
  • 3
    This doesn't really work, it should be `with open('infile','r') as fin, open ('outfile','w') as fout:` – pceccon Feb 28 '16 at 02:02
  • If I can ask. What if the original file is huge (let's say hundred of gigs)? Is there a solution that read a chunk of data to memory, store only the relevant rows and then continue to the next chunk? @Li-aungYip – Gluz Mar 21 '16 at 10:01
  • @gluz ask a new question and tag me in comments. – Li-aung Yip Mar 21 '16 at 12:16
  • Reference for anyone that reaches this: http://stackoverflow.com/questions/36132275/how-to-filter-specific-rows-from-a-huge-csv-file-using-python-script – Gluz Mar 21 '16 at 13:26
  • @pceccon I didn't see you comment before, came to the same solution, suggested an edit and got rejected. – Benjamin Feb 15 '20 at 09:00
  • Sorry, @LiveWireBT, did I reject something? Didn't get it. – pceccon Feb 17 '20 at 00:53
  • @pceccon No you did nothing wrong. I just wished I had seen your comment before. – Benjamin Feb 17 '20 at 05:45