Retardrino
Retardrino

Reputation: 63

How to delete lines from a file if they exceed x number of characters

How can I do something like this?

with open(r'C:\some_list.txt') as f:
    list = f.readlines()
for line in list:
    if line: #has more than x characters
        delete line

Upvotes: 1

Views: 1577

Answers (5)

SM Abu Taher Asif
SM Abu Taher Asif

Reputation: 2331

first save the lines in a list which will not be deleted by reading one by one:

the_list = []
with open(r'C:\some_list.txt', "r") as f:
    for line in f:
        #print(len(line))
        if (len(line)) < 50:#here I used 50 charecters
            the_list.append(line)

then write the list into your file:

with open(r'C:\some_list.txt', 'w') as f:
    for line in the_list:
        f.write(line)

if you don't want to use a list or the file is too big then try:

with open(r'C:\some_list.txt', "r") as f, open('new.txt', 'a') as fw:
    for line in f:
        if (len(line)) < 50:
            fw.write(line)

replace output.txt according to your need. Above code will read line by line from some_list.txt and then write in 'output.txt' if the line has less than 50 characters

Upvotes: 0

Apoorv Patne
Apoorv Patne

Reputation: 879

Conversely, it could be done like this:

# fname : file name
# x : number of characters or length
def delete_lines(fname = 'test.txt', x = 8):
    with open(fname, "r") as f:
        lines = f.readlines()
    with open(fname, "w") as f:
        for line in lines:
            if len(line) <= x:            
                f.write(line)

delete_lines()

Certainly, there are better ways of doing this.

Upvotes: 0

AKX
AKX

Reputation: 169051

If the file is reasonably small, the easiest way is to read it all in, filter, then write it all out.

with open(r'C:\some_list.txt')  as f:
    lines = f.readlines()

# Keep lines <= 10 chars long with a list comprehension
filtered_lines = [line for line in lines if len(line) > 10]

# Do what you like with the lines, e.g. write them out into another file:
with open(r'C:\filtered_list.txt', 'w') as f:
    for line in filtered_lines:
        f.write(line)

If you want to stream the matching lines into another file, that's even easier:

with open(r'C:\some_list.txt') as in_file, open(r'C:\filtered_list.txt', 'w') as out_file:
    for line in in_file:
        if len(line) <= 10:
            out_file.write(line)

Upvotes: 4

Masoud
Masoud

Reputation: 1280

You can read the file line by line, write the line in a new file if it pass the constrain (abandon other lines). For large files, its so efficient in terms of memory usage:

with open('file_r.txt', 'r') as file_r, open('file_w.txt', 'w') as file_w:
    thresh = 3
    for line in file_r:
        if len(line) < thresh:
            file_w.write(line)

Upvotes: 2

U13-Forward
U13-Forward

Reputation: 71580

Try (I do 3 as an example):

with open(r'C:\some_list.txt')  as f:
    l = [i for i in f if len(i) > 3]

I renamed list to l since list is a builtin.

Upvotes: 0

Related Questions