Python Forum
Removal of items in .txt using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removal of items in .txt using python
#1
Hi, I hv been self-learning Python on youtube videos. Today I visited the topic of file operation in which I would use Python to operate on notepad(txt files).
I managed to add stuff to a list of items using the write and append function. But I failed to make removal. For example,
Cat
Dog
Lion
Bear
These items are written in notepad. I want to delete one or more items from the list by user input. How can I do that? Thanks a lot in advance!
Reply
#2
(Aug-30-2022, 05:46 PM)nanakochan Wrote: I managed to add stuff to a list of items using the write and append function. But I failed to make removal. For example,
Cat
Dog
Lion
Bear
These items are written in notepad. I want to delete one or more items from the list by user input. How can I do that?

It's not going to be so different from the code you have; maybe a simple access mode change, but as we can't see what code you have, unless you post it, we'll be guessing, which may or may not help you.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
You cannot remove text from a file. This is a limitation of files, not Python. Instead you rewrite the file without the text you want to exclude. A text remove program usually has the following steps.
1. Open the file you want to modify in read mode.
2. Read all the lines from the file and save them as a list.
3. Close the file.
4. Open the file you want to modify in write mode. This deletes the contents of the file.
5. In a loop looking at each line your read in step 2:
a. Scan line for words you want to remove
b. Create a new line that doesn't contain the unwanted words.
c. Write the modified line to the file.

Sometimes this is done as a two file process. Reading a line from the original file and writing to a new file. When complete you can leave both files, or delete the old file and rename the new file using the old filename.

This is a pretty tricky programming problem, so don't expect to get it right the first try.
Reply
#4
When you say...

(Aug-30-2022, 05:46 PM)nanakochan Wrote: I managed to add stuff to a list of items using the write and append function.

... are you referring to the access modes w and a? Understand that if you open a file with access mode w, when you use the .write() method, you will overwrite the old file (and all that it contains) with a new version; whatever that is after your app have changed it, whereas access mode a will 'append' the changes to the old file, not replacing anything.

Does that make sense, or help?
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
Thank you for all your advice and suggestions.
I tried again. It still doesnt work. I failed to remove the item by user input. This is my code-

animalslist = open ('C:\\Users\\defaultuser100000\\animals.txt')
items = animalslist.read()
nlist = [items]
out = str(input("Enter items to be removed"))
if out == items:
    nlist.remove(out)
animalslist = open('C:\\Users\\defaultuser100000\\animals.txt', 'w' )
animalslist.write(items)
animalslist.close()
nlist = [items.replace('\n',' ') for items in nlist]
print ("You now have")
print (nlist)
print("in your list")
Gribouillis write Sep-01-2022, 03:04 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#6
You are creating a list with one sting in line 3.
['Cat\nDog\nLion\nBear']
Also if use with open then do no need to close() the file object.
Example can do it like this.
with open ('animals.txt') as f:
    items = [i.strip() for i in  f]

user_choice = input("Enter items to be removed: ")
items.remove(user_choice)

with open('animals.txt', 'w') as f_out:
    f_out.write('\n'.join(items))
The most common way is that just write to a new file.
Can use fileinput in standard library to do in place.
There is a small library in-place that make this prosses of modify the original file in place easier.
I have used some time before.
Example,it also nice that keep a backup of original file if needed.
import in_place

user_choice = input("Enter items to be removed: ")
with in_place.InPlace('animals.txt', backup='animals_bak.txt') as fp:
    items = [ani.strip() for ani in fp if user_choice not in ani]
    fp.write('\n'.join(items))
Reply
#7
You need to research what file.read() does. You need to research what a lot of Python commands do. An easy way to do this is run interactive Python.
I made a file that looks like this:
Output:
one two three
In a terminal I run python without specifying a .py file to start interactive python. Then I typed some commands and looked at what happened.
Output:
>>> file = open("test.txt", "r") >>> lines = file.read() >>> print(type(lines)) <class 'str'> >>> print(lines) one two three >>> print(repr(lines)) 'one\ntwo\nthree\n'
file.read() returned the contents of the file as a single string.
I could try replacing part of the string.
Output:
>>> new_lines = lines.replace("two", "") >>> print(new_lines) one three >>> print(repr(new_lines)) 'one\n\nthree\n'
That didn't work the way I wanted because I left in the newline after the word.
Output:
>>> new_lines = lines.replace("two\n", "") >>> print(new_lines) one three >>> print(repr(new_lines)) 'one\nthree\n'
That worked better. I don't like it, but it did work.

Next I am going to try file.readlines().
Output:
>>> file.close() >>> file = open("test.txt", "r") >>> lines = file.readlines() >>> file.close() >>> print(lines) ['one\n', 'two\n', 'three\n']
Now I get a list. I can remove an item from a list.
Output:
>>> new_lines = lines.remove("two") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: list.remove(x): x not in list
The problem is I entered "two", but the item in the list is "two\n".
Output:
>>> new_lines = lines.remove("two\n") >>> print(new_lines) None >>> print(lines) ['one\n', 'three\n']
What? Why None? I lookup the list.remove() function and see that the function removes the item from the list, modifying the list. In Python this is often referred to as "in place", meaning that a mutable object is modified by the function instead of creating a new object with different values. in place functions almost always return None.

Notice that "two\n" is missing from list when I print list.

But you probably don't want to type in "two\n" when you want to remove "two". To fix that problem, you need to remove the newline character from each of the strings.
Output:
>>> file = open("test.txt", "r") >>> lines = file.readlines() >>> file.close() >>> print(lines) ['one\n', 'two\n', 'three\n'] >>> lines = [line.strip() for line in lines] >>> print(lines) ['one', 'two', 'three'] >>> lines.remove("two") >>> lines ['one', 'three']
I keep forgetting that interactive python will print values without having to use a print command.

To remove the newline characters from each of the "lines", I used a list comprehension. Think of it as a shorthand way of writing a for loop. These are roughly equivalent.
for index, line in enumerate(lines):
    lines[index] = line.strip()

lines = [line.strip() for line in lines]
You might also want to remove words that use different case.
Output:
>>> file = open("test.txt", "r") >>> lines = [line.strip() for line in file.readlines()] >>> lines ['One', 'Two', 'Three'] >>> lines = [line for line in lines if line.lower() != "two"] >>> lines ['One', 'Three']
Here I use a list comprehension again. This time with a condition. I only include lines that don't match "two". To make the condition case insensitive I convert the line to lower case before doing the comparison.
Reply
#8
Not an answer to your question, but just put: Python tutorials in a search engine.

You will find many many sites that can help you. There are also tutorials here on this website!

I like realpython.com

https://www.w3schools.com/python lets you try things out directly

https://www.python.org/about/gettingstarted for absolute beginners

Then, if you get stuck, you can ask the experts here. (I'm not one of them!)

Always remember, there is usually more than one way to do what you want to do.

Have fun!
Reply
#9
I think that if one is familiar with sed it can be accomplished using subprocess quite easily (removing row 'Cat' from file animals.txt):

>>> import subprocess
>>> subprocess.run('cat animals.txt', shell=True)                 # file content before
Dog
Lion
Cat
Bear
CompletedProcess(args='cat animals.txt', returncode=0)
>>> subprocess.Popen(['sed', '-i', '', '/Cat/d', 'animals.txt'])   # inplace deletion with sed
<Popen: returncode: None args: ['sed', '-i', '', '/Cat/d', 'animals.txt']>
>>> subprocess.run('cat animals.txt', shell=True)                # file content after
Dog
Lion
Bear
CompletedProcess(args='cat animals.txt', returncode=0)
Edit: I noticed, that using cat (concatenate and print files) and Cat can be confusing. Sorry about that.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get all items in SharePoint recycle bin by using sharepy library in Python? QuangHuynh 2 353 Apr-10-2024, 03:09 PM
Last Post: SandraYokum
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 207 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Removal of duplicates teebee891 1 1,806 Feb-01-2021, 12:06 PM
Last Post: jefsummers
  concatenating 2 items at a time in a python list K11 3 2,356 Oct-21-2020, 09:34 AM
Last Post: buran
  how to use items combobox in table name sqlite in python hampython 1 2,698 May-24-2020, 02:17 AM
Last Post: Larz60+
  Access list items in Python kamaleon 2 2,365 Dec-31-2019, 11:10 AM
Last Post: kamaleon
  Vertical Seam Removal scott14 0 1,951 Dec-27-2018, 03:03 AM
Last Post: scott14
  Python find the minimum length of string to differentiate dictionary items zydjohn 3 3,648 Mar-03-2018, 05:23 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020