Hi Pythonist,
I write the code in Python for search and replace the text in all XML files at current directory.
But I'm unable to find and replace the text "" as empty in all XML files. Here I want to remove the text:
z_{1 - \alpha }^2 \,\sigma _{{\rm SCR}}^2 = \sum\limits_{i = 1}^n {z_{1 - \alpha }^2 \,\sigma _i^2 } + 2\sum\limits_{i \ne }^n {\sum\limits_j^n {z_{1 - \alpha }^2 \,\rho _{ij} \,\sigma _i \,\sigma _j } } ,
as
empty
ie. re.sub(r"(.*)",r"", filePath)
whats wrong with this code and explain how and where to use the script.
And also I want to replace all starting with as and as within the element 1a for all XML files in the current directory.
Here I want to replace the text:
risk modules:1az1-α2 σSCR2=∑i=1nz1-α2 σi2+2∑i≠n∑jnz1-α2 ρij σi σj,z_{1 - \alpha }^2 \,\sigma _{{\rm SCR}}^2 = \sum\limits_{i = 1}^n {z_{1 - \alpha }^2 \,\sigma _i^2 } + 2\sum\limits_{i \ne }^n {\sum\limits_j^n {z_{1 - \alpha }^2 \,\rho _{ij} \,\sigma _i \,\sigma _j } } ,where
as
risk modules:1az1-α2 σSCR2=∑i=1nz1-α2 σi2+2∑i≠n∑jnz1-α2 ρij σi σj,where
Pls. explain how and where to use the script.
Here my main python script is:
import os, sys, shutil
mydir= os.getcwd()
#makedir = os.mkdir('out')
#path = os.getcwd()
#filename = glob.glob(os.path.join(path, '*.xml') )
findreplace = [
('–','–'),
('’','É'),
]
def replaceStringInFile(filePath):
"replaces all findStr by repStr in file filePath"
tempName=filePath+'xml'
# backupName=filePath+'~~'
input = open(filePath)
output = open(tempName,'w')
inF = open(filePath,'r')
s=unicode(inF.read())
inF.close()
for couple in findreplace:
outtext=s.replace(couple[0],couple[1])
s=outtext
outF = open(tempName,'wb')
outF.write(outtext.encode('utf-8'))
outF.close()
# shutil.copy2(filePath,backupName)
# os.remove(filePath)
# os.rename(tempName,filePath)
def myfun(dummy, dirr, filess):
for child in filess:
if '.xml' == os.path.splitext(child)[1] and os.path.isfile(dirr+'/'+child):
replaceStringInFile(dirr+'/'+child)
os.path.walk(mydir, myfun, 3)
Can anyone help me regarding the above 2 points.
Thanks in advance
Naren