franco_b
franco_b

Reputation: 878

Filter rows from text file

I have to clear a txt file (test.txt), removing row that start with '(' or null rows and then rename it (test_ok.txt)

I use this function:

def clearfile(filename):
    for row in (open (filename, 'r')).readlines():
        if row[0][0]<>'\n' and riga[0][0]<>'(' :
            out_file = open('%s_%s%s' %(os.path.splitext(filename)[0],'ok',os.path.splitext(os.path.basename(filename))[1]) , 'a')
            out_file.write(riga) 
            out_file.close()
    return out_file

It works but is very slow. Any tips to optmize?

This is my new function:

def clearfile(filename):
    with open (filename, 'r') as a, open('%s_%s%s' %(os.path.splitext(filename)[0],'ok',os.path.splitext(os.path.basename(filename))[1]) , 'a') as b:
        for row in (a).readlines():
            if row[0][0]!='\n' and row[0][0]!= '(' :
                b.write(row) 
        b.close

Upvotes: 1

Views: 542

Answers (2)

moooeeeep
moooeeeep

Reputation: 32512

Try something like this:

import os
def clear_and_move(infname, outfname):
    with open(infname, 'r') as inf, open(outfname, 'w+') as outf:
        # write new file as a copy of the first
        # skip lines that contain only whitespace or start with '('
        outf.writelines(line for line in inf 
                        if line.strip() and not line.startswith('('))
    # delete the original file
    os.remove(infname)

Upvotes: 3

Anubhav C
Anubhav C

Reputation: 2651

Why open/close the file more than once?

def clearfile(filename):
    out_file = open('%s_%s%s' %(os.path.splitext(filename)[0], 'ok', os.path.splitext(os.path.basename(filename))[1]) , 'a')
    for row in (open (filename, 'r')).readlines():
        if row[0][0]!= '\n' and riga[0][0]!='(' :
            out_file.write(riga) 
    out_file.close()
    return out_file

Upvotes: 4

Related Questions