| | |
Search and replace multiple file
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2007
Posts: 1
Reputation:
Solved Threads: 0
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 "<tex>" as empty in all XML files. Here I want to remove the text:
<tex>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 } } ,</tex>
as
empty
ie. re.sub(r"<tex>(.*)</tex>",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 <mtml> as <mml:mathml> and <m...> as <mml:m...> within the element <fd id="1" cnt="1"><no>1a</no> for all XML files in the current directory.
Here I want to replace the text:
risk modules:<fd id="1" cnt="1"><no>1a</no><mtml><mrow><msubsup><mi><mi>z</mi></mi><mrow><mn>1</mn><mo>-</mo><mi><mi>α</mi></mi></mrow><mn>2</mn></msubsup><mi> </mi><msubsup><mi><mi>σ</mi></mi><mrow><mtext>SCR</mtext></mrow><mn>2</mn></msubsup><mo>=</mo><munderover><mo>∑</mo><mrow><mi><mi>i</mi></mi><mo>=</mo><mn>1</mn></mrow><mi><mi>n</mi></mi></munderover><mrow><msubsup><mi><mi>z</mi></mi><mrow><mn>1</mn><mo>-</mo><mi><mi>α</mi></mi></mrow><mn>2</mn></msubsup><mi> </mi><msubsup><mi><mi>σ</mi></mi><mi><mi>i</mi></mi><mn>2</mn></msubsup></mrow><mo>+</mo><mn>2</mn><munderover><mo>∑</mo><mrow><mi><mi>i</mi></mi><mo>≠</mo></mrow><mi><mi>n</mi></mi></munderover><mrow><munderover><mo>∑</mo><mi><mi>j</mi></mi><mi><mi>n</mi></mi></munderover><mrow><msubsup><mi><mi>z</mi></mi><mrow><mn>1</mn><mo>-</mo><mi><mi>α</mi></mi></mrow><mn>2</mn></msubsup><mi> </mi><msub><mi><mi>ρ</mi></mi><mrow><mi><mi>i</mi><mi>j</mi></mi></mrow></msub><mi> </mi><msub><mi><mi>σ</mi></mi><mi><mi>i</mi></mi></msub><mi> </mi><msub><mi><mi>σ</mi></mi><mi><mi>j</mi></mi></msub></mrow></mrow><mtext><mo>,</mo></mtext></mrow></mtml><tex>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 } } ,</tex></fd>where
as
risk modules:<fd id="1" cnt="1"><no>1a</no><mml:mathml><mml:row><mml:subsup><mml:i><mml:i>z</mml:i></mml:i><mml:row><mml:n>1</mml:n><mml:o>-</mml:o><mml:i><mml:i>α</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i> </mml:i><mml:subsup><mml:i><mml:i>σ</mml:i></mml:i><mml:row><mml:text>SCR</mml:text></mml:row><mml:n>2</mml:n></mml:subsup><mml:o>=</mml:o><mml:underover><mml:o>∑</mml:o><mml:row><mml:i><mml:i>i</mml:i></mml:i><mml:o>=</mml:o><mml:n>1</mml:n></mml:row><mml:i><mml:i>n</mml:i></mml:i></mml:underover><mml:row><mml:subsup><mml:i><mml:i>z</mml:i></mml:i><mml:row><mml:n>1</mml:n><mml:o>-</mml:o><mml:i><mml:i>α</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i> </mml:i><mml:subsup><mml:i><mml:i>σ</mml:i></mml:i><mml:i><mml:i>i</mml:i></mml:i><mml:n>2</mml:n></mml:subsup></mml:row><mml:o>+</mml:o><mml:n>2</mml:n><mml:underover><mml:o>∑</mml:o><mml:row><mml:i><mml:i>i</mml:i></mml:i><mml:o>≠</mml:o></mml:row><mml:i><mml:i>n</mml:i></mml:i></mml:underover><mml:row><mml:underover><mml:o>∑</mml:o><mml:i><mml:i>j</mml:i></mml:i><mml:i><mml:i>n</mml:i></mml:i></mml:underover><mml:row><mml:subsup><mml:i><mml:i>z</mml:i></mml:i><mml:row><mml:n>1</mml:n><mml:o>-</mml:o><mml:i><mml:i>α</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i> </mml:i><mml:sub><mml:i><mml:i>ρ</mml:i></mml:i><mml:row><mml:i><mml:i>i</mml:i><mml:i>j</mml:i></mml:i></mml:row></mml:sub><mml:i> </mml:i><mml:sub><mml:i><mml:i>σ</mml:i></mml:i><mml:i><mml:i>i</mml:i></mml:i></mml:sub><mml:i> </mml:i><mml:sub><mml:i><mml:i>σ</mml:i></mml:i><mml:i><mml:i>j</mml:i></mml:i></mml:sub></mml:row></mml:row><mml:text><mml:o>,</mml:o></mml:text></mml:row></mml:mathml>
</fd>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
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 "<tex>" as empty in all XML files. Here I want to remove the text:
<tex>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 } } ,</tex>
as
empty
ie. re.sub(r"<tex>(.*)</tex>",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 <mtml> as <mml:mathml> and <m...> as <mml:m...> within the element <fd id="1" cnt="1"><no>1a</no> for all XML files in the current directory.
Here I want to replace the text:
risk modules:<fd id="1" cnt="1"><no>1a</no><mtml><mrow><msubsup><mi><mi>z</mi></mi><mrow><mn>1</mn><mo>-</mo><mi><mi>α</mi></mi></mrow><mn>2</mn></msubsup><mi> </mi><msubsup><mi><mi>σ</mi></mi><mrow><mtext>SCR</mtext></mrow><mn>2</mn></msubsup><mo>=</mo><munderover><mo>∑</mo><mrow><mi><mi>i</mi></mi><mo>=</mo><mn>1</mn></mrow><mi><mi>n</mi></mi></munderover><mrow><msubsup><mi><mi>z</mi></mi><mrow><mn>1</mn><mo>-</mo><mi><mi>α</mi></mi></mrow><mn>2</mn></msubsup><mi> </mi><msubsup><mi><mi>σ</mi></mi><mi><mi>i</mi></mi><mn>2</mn></msubsup></mrow><mo>+</mo><mn>2</mn><munderover><mo>∑</mo><mrow><mi><mi>i</mi></mi><mo>≠</mo></mrow><mi><mi>n</mi></mi></munderover><mrow><munderover><mo>∑</mo><mi><mi>j</mi></mi><mi><mi>n</mi></mi></munderover><mrow><msubsup><mi><mi>z</mi></mi><mrow><mn>1</mn><mo>-</mo><mi><mi>α</mi></mi></mrow><mn>2</mn></msubsup><mi> </mi><msub><mi><mi>ρ</mi></mi><mrow><mi><mi>i</mi><mi>j</mi></mi></mrow></msub><mi> </mi><msub><mi><mi>σ</mi></mi><mi><mi>i</mi></mi></msub><mi> </mi><msub><mi><mi>σ</mi></mi><mi><mi>j</mi></mi></msub></mrow></mrow><mtext><mo>,</mo></mtext></mrow></mtml><tex>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 } } ,</tex></fd>where
as
risk modules:<fd id="1" cnt="1"><no>1a</no><mml:mathml><mml:row><mml:subsup><mml:i><mml:i>z</mml:i></mml:i><mml:row><mml:n>1</mml:n><mml:o>-</mml:o><mml:i><mml:i>α</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i> </mml:i><mml:subsup><mml:i><mml:i>σ</mml:i></mml:i><mml:row><mml:text>SCR</mml:text></mml:row><mml:n>2</mml:n></mml:subsup><mml:o>=</mml:o><mml:underover><mml:o>∑</mml:o><mml:row><mml:i><mml:i>i</mml:i></mml:i><mml:o>=</mml:o><mml:n>1</mml:n></mml:row><mml:i><mml:i>n</mml:i></mml:i></mml:underover><mml:row><mml:subsup><mml:i><mml:i>z</mml:i></mml:i><mml:row><mml:n>1</mml:n><mml:o>-</mml:o><mml:i><mml:i>α</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i> </mml:i><mml:subsup><mml:i><mml:i>σ</mml:i></mml:i><mml:i><mml:i>i</mml:i></mml:i><mml:n>2</mml:n></mml:subsup></mml:row><mml:o>+</mml:o><mml:n>2</mml:n><mml:underover><mml:o>∑</mml:o><mml:row><mml:i><mml:i>i</mml:i></mml:i><mml:o>≠</mml:o></mml:row><mml:i><mml:i>n</mml:i></mml:i></mml:underover><mml:row><mml:underover><mml:o>∑</mml:o><mml:i><mml:i>j</mml:i></mml:i><mml:i><mml:i>n</mml:i></mml:i></mml:underover><mml:row><mml:subsup><mml:i><mml:i>z</mml:i></mml:i><mml:row><mml:n>1</mml:n><mml:o>-</mml:o><mml:i><mml:i>α</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i> </mml:i><mml:sub><mml:i><mml:i>ρ</mml:i></mml:i><mml:row><mml:i><mml:i>i</mml:i><mml:i>j</mml:i></mml:i></mml:row></mml:sub><mml:i> </mml:i><mml:sub><mml:i><mml:i>σ</mml:i></mml:i><mml:i><mml:i>i</mml:i></mml:i></mml:sub><mml:i> </mml:i><mml:sub><mml:i><mml:i>σ</mml:i></mml:i><mml:i><mml:i>j</mml:i></mml:i></mml:sub></mml:row></mml:row><mml:text><mml:o>,</mml:o></mml:text></mml:row></mml:mathml>
</fd>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
•
•
Join Date: Mar 2006
Posts: 18
Reputation:
Solved Threads: 0
hi, i have used the method explaned in following link, and this helped me a lot, i am sure that this also ll help you..
http://aspn.activestate.com/ASPN/Coo...n/Recipe/52250
http://aspn.activestate.com/ASPN/Coo...n/Recipe/52250
Best wishes,
Vivek Sharma
Zope/Python Developer
Vivek Sharma
Zope/Python Developer
•
•
Join Date: Jan 2009
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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 "<tex>" as empty in all XML files. Here I want to remove the text:
<tex>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 } } ,</tex>
as
empty
ie. re.sub(r"<tex>(.*)</tex>",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 <mtml> as <mml:mathml> and <m...> as <mml:m...> within the element <fd id="1" cnt="1"><no>1a</no> for all XML files in the current directory.
Here I want to replace the text:
risk modules:<fd id="1" cnt="1"><no>1a</no><mtml><mrow><msubsup><mi><mi>z</mi></mi><mrow><mn>1</mn><mo>-</mo><mi><mi>α</mi></mi></mrow><mn>2</mn></msubsup><mi> </mi><msubsup><mi><mi>σ</mi></mi><mrow><mtext>SCR</mtext></mrow><mn>2</mn></msubsup><mo>=</mo><munderover><mo>∑</mo><mrow><mi><mi>i</mi></mi><mo>=</mo><mn>1</mn></mrow><mi><mi>n</mi></mi></munderover><mrow><msubsup><mi><mi>z</mi></mi><mrow><mn>1</mn><mo>-</mo><mi><mi>α</mi></mi></mrow><mn>2</mn></msubsup><mi> </mi><msubsup><mi><mi>σ</mi></mi><mi><mi>i</mi></mi><mn>2</mn></msubsup></mrow><mo>+</mo><mn>2</mn><munderover><mo>∑</mo><mrow><mi><mi>i</mi></mi><mo>≠</mo></mrow><mi><mi>n</mi></mi></munderover><mrow><munderover><mo>∑</mo><mi><mi>j</mi></mi><mi><mi>n</mi></mi></munderover><mrow><msubsup><mi><mi>z</mi></mi><mrow><mn>1</mn><mo>-</mo><mi><mi>α</mi></mi></mrow><mn>2</mn></msubsup><mi> </mi><msub><mi><mi>ρ</mi></mi><mrow><mi><mi>i</mi><mi>j</mi></mi></mrow></msub><mi> </mi><msub><mi><mi>σ</mi></mi><mi><mi>i</mi></mi></msub><mi> </mi><msub><mi><mi>σ</mi></mi><mi><mi>j</mi></mi></msub></mrow></mrow><mtext><mo>,</mo></mtext></mrow></mtml><tex>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 } } ,</tex></fd>where
as
risk modules:<fd id="1" cnt="1"><no>1a</no><mml:mathml><mml:row><mml:subsup><mml:i><mml:i>z</mml:i></mml:i><mml:row><mml:n>1</mml:n><mml:o>-</mml:o><mml:i><mml:i>α</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i> </mml:i><mml:subsup><mml:i><mml:i>σ</mml:i></mml:i><mml:row><mml:text>SCR</mml:text></mml:row><mml:n>2</mml:n></mml:subsup><mml:o>=</mml:o><mml:underover><mml:o>∑</mml:o><mml:row><mml:i><mml:i>i</mml:i></mml:i><mml:o>=</mml:o><mml:n>1</mml:n></mml:row><mml:i><mml:i>n</mml:i></mml:i></mml:underover><mml:row><mml:subsup><mml:i><mml:i>z</mml:i></mml:i><mml:row><mml:n>1</mml:n><mml:o>-</mml:o><mml:i><mml:i>α</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i> </mml:i><mml:subsup><mml:i><mml:i>σ</mml:i></mml:i><mml:i><mml:i>i</mml:i></mml:i><mml:n>2</mml:n></mml:subsup></mml:row><mml:o>+</mml:o><mml:n>2</mml:n><mml:underover><mml:o>∑</mml:o><mml:row><mml:i><mml:i>i</mml:i></mml:i><mml:o>≠</mml:o></mml:row><mml:i><mml:i>n</mml:i></mml:i></mml:underover><mml:row><mml:underover><mml:o>∑</mml:o><mml:i><mml:i>j</mml:i></mml:i><mml:i><mml:i>n</mml:i></mml:i></mml:underover><mml:row><mml:subsup><mml:i><mml:i>z</mml:i></mml:i><mml:row><mml:n>1</mml:n><mml:o>-</mml:o><mml:i><mml:i>α</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i> </mml:i><mml:sub><mml:i><mml:i>ρ</mml:i></mml:i><mml:row><mml:i><mml:i>i</mml:i><mml:i>j</mml:i></mml:i></mml:row></mml:sub><mml:i> </mml:i><mml:sub><mml:i><mml:i>σ</mml:i></mml:i><mml:i><mml:i>i</mml:i></mml:i></mml:sub><mml:i> </mml:i><mml:sub><mml:i><mml:i>σ</mml:i></mml:i><mml:i><mml:i>j</mml:i></mml:i></mml:sub></mml:row></mml:row><mml:text><mml:o>,</mml:o></mml:text></mml:row></mml:mathml>
</fd>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
This post is over a year old. Just let it die.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
![]() |
Similar Threads
- Replace words in a file (Python)
- Search string in a text file (C)
- New User (C)
Other Threads in the Python Forum
- Previous Thread: Python modules ..
- Next Thread: I need help please
Views: 2067 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
alarm aliased application beginner calculator casino character code command cursor cx-freeze definedlines development dictionary dynamic error event examples excel exe file filename float format ftp function google graphics gui homework ideas import input java launcher line linux list lists logging loop matching microphone mouse movingimageswithpygame newb number numbers obexftp output parsing path permissions phonebook port prime program programming projects py2exe pygame pygtk pyqt pysimplewizard python random recursion recursive refresh return reverse scrolledtext shebang simple skinning sprite ssh statistics string strings table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip windows wxpython





