Tutorial Dictionary Use in real life example - Printable Version +- Red Security (https://redsecurity.info/cc) +-- Forum: Programming (https://redsecurity.info/cc/forumdisplay.php?fid=5) +--- Forum: Python (https://redsecurity.info/cc/forumdisplay.php?fid=9) +--- Thread: Tutorial Dictionary Use in real life example (/showthread.php?tid=7972) |
Dictionary Use in real life example - Covid-19 - 06-16-2022 Change a word in text File using Dictionary: Hi guys, I hope you are doing well. In today’s thread I am going to show you how you can use a python dictionary data structure to change a word in a text file so without wasting any time let’s get started. First of all we have to load our text file in our programCode: file = open('SomeText.txt') After that now we will store it into a string named ‘text’ Code: text = file.read() Now we will create a dictionary that stores the existing word as key and the word that we want to add as value just like this.. Code: d = {} So now we want to change left with ‘__left__’ and so on. You can change the values by your wish and now we will replace these words in our string ‘text’.. Code: for key in d: And now if we want to print the text we will get the new string.. Code: print(text) So the whole code will be like this Code: file = open('SomeText.txt') The result of this code will look like this... So that's it for now. If you have any questions related to python you can ask in the comment section or directly DM me. Thank you! "Keep Good care of your Health Allah Hafiz and Good Bye Till the next post" |