![]() |
| ||
| Replace words in a file I need to read in a text file, replace selected words and write the changed text back out to a file. Is there an easy way with Python? |
| ||
| Re: Replace words in a file Quote:
1) reading with for loop o = open("output","a") #open for append2) with while loop f = open("file")3) using regular expression and reading the whole file into memory import re If you don't want to explicitly declare file handles for writing to output file, you can use fileinput module, to modify file in place import fileinput |
| ||
| Re: Replace words in a file Thanks ghostdog! I will have to play with those code ideas. I need to replace several different words in the text file. |
| ||
| Re: Replace words in a file Here is an example, where the target and replacement words are in a dictionary. The Python module re is used to do the gruntwork ... # read a text file, replace multiple words specified in a dictionary |
| ||
| Re: Replace words in a file Thanks again, the last code example will work for me. I will have to read up on the module re to digest it all. |
| ||
| Re: Replace words in a file If i wanted to read the words to be replaced from another text file and then replace them wat modifications to the code would i have to make?? |
| All times are GMT -4. The time now is 8:59 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC