Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Efficient way to replace parts of a word in a document where the replacement words are defined in a excel

$
0
0

I have a .txt file which is hugeI have read it using readlines() in python

Sample:

lines = ["XX_A_Name, A_Bad_Joke = '67' , \n", "A quick Black.Jack fox JJ_Value over XX_A_Name a.lazy.dog\n"]

Replacements are defined in excel file:

OldName    New_replacementXX_A_        ZZ_B_A_Bad_Joke   C_Good_JokeBlack.Jack   Yellow.flowerJJ_Value     KK_Suma.lazy.dog   very.huge

I want to read the excel file and replace all the occurances of the values which are listed in OldName column from the txt file.

For eg: the sample output should be like:

["ZZ_B_Name, C_Good_Joke = '67' , \n", "A quick Yellow.flower fox KK_Sum over ZZ_B_Name very.huge\n"]

I want to create a function for this. because this is just sample portion of the txt file I've pasted here. there are multiple .txt files which has to be load, replaced and saved as a new .txt file.

I am seeking help for a efficient and a quick way to do this.

Reading excel can be done using pd.read_excel() and re package can be used for replace(). but I am not able to understand how everything will fit together.


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>