How to delete a user specified line from a text file in python? (The file contains different lines with dictionary) | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

How to delete a user specified line from a text file in python? (The file contains different lines with dictionary)

There is a text file my_file.txt which contains several rows of dictionary items {big: small} {high: low} {thin: thick} How to delete any item from this file that user chooses?

26th Dec 2022, 4:55 PM
Neethu Nath
Neethu Nath - avatar
18 Respostas
+ 3
To delete a specified line from a text file can be done for example in this way: https://code.sololearn.com/c6ej3sFhb0aj/?ref=app But for a very big number of data records it would be recomended to use a database for the application.
28th Dec 2022, 11:49 AM
JaScript
JaScript - avatar
+ 2
I see . Thats one way to do it glad you figured it out
27th Dec 2022, 4:18 PM
Ion Kare
Ion Kare - avatar
+ 1
with open('scratch_4.txt', 'r+') as file: with open('new.txt', 'w') as new_file: for line in file: line = line.strip() if remove_item in line: continue: new_file.write(line)
26th Dec 2022, 8:00 PM
Neethu Nath
Neethu Nath - avatar
0
Title asks how to delete a line from a text file but in Description it asks how to delete any item - presumably was referring to the dictionary item - not the line. Please clarify or edit the post to confirm which one it is, even better ...
26th Dec 2022, 5:18 PM
Ipang
0
can the user chooseJust 1 or multiple, if the user pick big can he also pick small or is big the key and small the value which means big gets deleted as a whole?
26th Dec 2022, 5:41 PM
Ion Kare
Ion Kare - avatar
0
Only one item can be chosen at a time. Input will be key only. Using key, I map value and identify the item.
26th Dec 2022, 5:43 PM
Neethu Nath
Neethu Nath - avatar
0
Did you try to do it ? Neethu Nath
26th Dec 2022, 7:49 PM
Ion Kare
Ion Kare - avatar
0
I tried to use line.strip() but I cannot check if a dictionary in line
26th Dec 2022, 7:52 PM
Neethu Nath
Neethu Nath - avatar
0
Can you show ?
26th Dec 2022, 7:53 PM
Ion Kare
Ion Kare - avatar
0
It is giving error TypeError: 'in <string>' requires string as left operand, not dict
26th Dec 2022, 7:55 PM
Neethu Nath
Neethu Nath - avatar
0
Youre pretty close remove the + from read mode, for line in file.readlines() remove reset of line below and just print line
26th Dec 2022, 8:06 PM
Ion Kare
Ion Kare - avatar
0
then you can continue from there repost your code
26th Dec 2022, 8:07 PM
Ion Kare
Ion Kare - avatar
0
Oh forgot it might overwite file so it may not print anything
26th Dec 2022, 8:09 PM
Ion Kare
Ion Kare - avatar
0
https://pastebin.com/Cq9d3HRY pw: booker Change text_file to w.e your file is
26th Dec 2022, 8:20 PM
Ion Kare
Ion Kare - avatar
0
I changed the logic completely and solved this
27th Dec 2022, 4:06 PM
Neethu Nath
Neethu Nath - avatar
0
Awsome ! :D can i see your newly logic ?
27th Dec 2022, 4:14 PM
Ion Kare
Ion Kare - avatar
0
I used single dictionary to add all inputs instead of each item in a row
27th Dec 2022, 4:16 PM
Neethu Nath
Neethu Nath - avatar
0
Thank you for trying to help me
27th Dec 2022, 4:20 PM
Neethu Nath
Neethu Nath - avatar