Filtering content of a text file

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Jul 27 12:15:25 CEST 2007


Ira.Kovac at gmail.com a écrit :
> Hello All,
> 
> I'd greatly appreciate if you can take a look at the task I need help
> with.
> 
> It'd be outstanding if someone can provide some sample Python code.

No problem. It's 600 euro per day. Do I send you the contract ?

> -------------------------------------------------------------------------------
> Problem
> -------------------------------------------------------------------------------
> 
> I am working with 30K+ record datasets in flat file format (.txt) that
> look like this:
> 
> //-+alibaba sinage
> //-+amra damian//_9
> //-+anix anire//_
> //-+borom
> //-+bokima sun drane
> //-+ciren
> //-+cop calestieon eded
> //-+ciciban
> //-+drago kimano sole
> 
> 
> The records start with the same string (in the example //-+) wich is
> followed by another string of characters taht's changing from record
> to record.
> 
> I am working on one file at the time and for each file I need to be
> able to do the following:
> 
> a) By looping thru the file the program should isolate all records
> that have letter a following the //-+
> b) The isolated dataset will contain only records that start with //-
> +a
> c) Save the isolated dataset as flat flat text file named a.txt
> d) Repeat a), b) and c) for all letters of english alphabet (a thru z)
> and numerical values (0 thru 9)
> 

This really looks like homework, and asking people to do your homework 
for you is a pretty bad idea. On most newsgroup, the answer would stop 
here, but c.l.py is a very friendly place, so I'll give you a couple 
starting points:

1/ for char in "abc":
        print "char is %s" % char
        print "//-+%s" % char

2/ for line in open('somefile'):
        print line

3/ print "//-+alibaba sinage"[4:]

4/ print "//-+alibaba sinage"[4:].startswith('a')

5/ data = []
    data.append("//-+alibaba sinage\n")
    data.append("//-+amra damian//_9\n")
    print "".join(data)

6/ f = open('someotherfile.txt', 'w')
    f.write("line1\nline2\nline3\n")
    f.close()

This is all you need to know to complete your task.



More information about the Python-list mailing list