I
I
Ivan Yakushenko2019-06-24 14:38:21
Python
Ivan Yakushenko, 2019-06-24 14:38:21

Why does the script for deleting lines from txt not work?

A small code to remove spent links from the general list:

with open("lots.txt", "r") as f:
    lots = f.readlines()
    print('All lots: {}\nTo removing: {}'.format(len(lots), len(loaded)))
    for url in loaded:
        try:
            lots.remove(url.split(' ')[-1])
            print('Removed: {}'.format(url.split(' ')[-1]))
        except ValueError:
            pass
    with open("upd_lots.txt", "w") as new_f:
        for lot in lots:        
            new_f.write(lot)

>>> print(type(lots))
<class 'list'>
>>> print(type(loaded))
<class 'list'>
>>>

When running the script on the command line:
All lots:
7812589

Nothing else: no errors, no output from print('Removed: {}'.format(url.split(' ')[-1])) .
In the task manager, the script successfully ate ~ 1GB of RAM and spins the processor a little in the region of 15-17%.
In this regard, I can’t understand: it is frozen, or simply for some reason print('Removed: {}'.format(url.split(' ')[-1])) is not displayed . I tried both in WPS and on the command line - the result is the same.
UPD. Added to except print('There is no such line: {}'.format(url.split(' ')[-1])) it turned out that it does not find the line. I printed out one element from lots and loaded - into lots at the end of the special line. the \n character, apparently it interferes. I just don't know how to remove it, can someone tell me?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2019-06-24
@kshnkvn

I needed to read the line like this: readlines() leaves behind \n .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question