B
B
bychok3002017-02-14 14:05:40
Python
bychok300, 2017-02-14 14:05:40

How to find and remove all required lines in python?

There are many links in the file.
How to run through the file and delete everything that starts with http or https until the end of the url and no more? Without using an additional file.
I know how to do it in bash but not in python.
Tried like this:

for files in what_in_dir:
    o = open(i,'a')
    data = open(i).read()
    o.write( re.sub(r'^http://*$','',data)  )
    o.close()

did not help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-02-14
@bychok300

clean_list = []
with open('input.txt', 'r', encoding="utf-8") as f_input:
    for l in f_input:
        if not l.startswith('http'):
            clean_list.append(l)

with open('input.txt', 'w', encoding="utf-8") as f_out:
    f_out.write('\n'.join(clean_list))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question