943,650 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 3541
  • Python RSS
Sep 17th, 2007
0

Search and replace multiple file

Expand Post »
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>&#x03B1;</mi></mi></mrow><mn>2</mn></msubsup><mi>&#x2009;</mi><msubsup><mi><mi>&#x03C3;</mi></mi><mrow><mtext>SCR</mtext></mrow><mn>2</mn></msubsup><mo>=</mo><munderover><mo>&#x2211;</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>&#x03B1;</mi></mi></mrow><mn>2</mn></msubsup><mi>&#x2009;</mi><msubsup><mi><mi>&#x03C3;</mi></mi><mi><mi>i</mi></mi><mn>2</mn></msubsup></mrow><mo>+</mo><mn>2</mn><munderover><mo>&#x2211;</mo><mrow><mi><mi>i</mi></mi><mo>&#x2260;</mo></mrow><mi><mi>n</mi></mi></munderover><mrow><munderover><mo>&#x2211;</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>&#x03B1;</mi></mi></mrow><mn>2</mn></msubsup><mi>&#x2009;</mi><msub><mi><mi>&#x03C1;</mi></mi><mrow><mi><mi>i</mi><mi>j</mi></mi></mrow></msub><mi>&#x2009;</mi><msub><mi><mi>&#x03C3;</mi></mi><mi><mi>i</mi></mi></msub><mi>&#x2009;</mi><msub><mi><mi>&#x03C3;</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>&#x03B1;</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i>&#x2009;</mml:i><mml:subsup><mml:i><mml:i>&#x03C3;</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>&#x2211;</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>&#x03B1;</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i>&#x2009;</mml:i><mml:subsup><mml:i><mml:i>&#x03C3;</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>&#x2211;</mml:o><mml:row><mml:i><mml:i>i</mml:i></mml:i><mml:o>&#x2260;</mml:o></mml:row><mml:i><mml:i>n</mml:i></mml:i></mml:underover><mml:row><mml:underover><mml:o>&#x2211;</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>&#x03B1;</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i>&#x2009;</mml:i><mml:sub><mml:i><mml:i>&#x03C1;</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>&#x2009;</mml:i><mml:sub><mml:i><mml:i>&#x03C3;</mml:i></mml:i><mml:i><mml:i>i</mml:i></mml:i></mml:sub><mml:i>&#x2009;</mml:i><mml:sub><mml:i><mml:i>&#x03C3;</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 = [
('&ndash;','–'),
('&rsquo;','É'),
]

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
narendran_9 is offline Offline
1 posts
since Sep 2007
Oct 3rd, 2007
0

Re: Search and replace multiple file

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sharma_vivek82 is offline Offline
18 posts
since Mar 2006
Feb 7th, 2009
0

Re: Search and replace multiple file

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>&#x03B1;</mi></mi></mrow><mn>2</mn></msubsup><mi>&#x2009;</mi><msubsup><mi><mi>&#x03C3;</mi></mi><mrow><mtext>SCR</mtext></mrow><mn>2</mn></msubsup><mo>=</mo><munderover><mo>&#x2211;</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>&#x03B1;</mi></mi></mrow><mn>2</mn></msubsup><mi>&#x2009;</mi><msubsup><mi><mi>&#x03C3;</mi></mi><mi><mi>i</mi></mi><mn>2</mn></msubsup></mrow><mo>+</mo><mn>2</mn><munderover><mo>&#x2211;</mo><mrow><mi><mi>i</mi></mi><mo>&#x2260;</mo></mrow><mi><mi>n</mi></mi></munderover><mrow><munderover><mo>&#x2211;</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>&#x03B1;</mi></mi></mrow><mn>2</mn></msubsup><mi>&#x2009;</mi><msub><mi><mi>&#x03C1;</mi></mi><mrow><mi><mi>i</mi><mi>j</mi></mi></mrow></msub><mi>&#x2009;</mi><msub><mi><mi>&#x03C3;</mi></mi><mi><mi>i</mi></mi></msub><mi>&#x2009;</mi><msub><mi><mi>&#x03C3;</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>&#x03B1;</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i>&#x2009;</mml:i><mml:subsup><mml:i><mml:i>&#x03C3;</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>&#x2211;</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>&#x03B1;</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i>&#x2009;</mml:i><mml:subsup><mml:i><mml:i>&#x03C3;</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>&#x2211;</mml:o><mml:row><mml:i><mml:i>i</mml:i></mml:i><mml:o>&#x2260;</mml:o></mml:row><mml:i><mml:i>n</mml:i></mml:i></mml:underover><mml:row><mml:underover><mml:o>&#x2211;</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>&#x03B1;</mml:i></mml:i></mml:row><mml:n>2</mml:n></mml:subsup><mml:i>&#x2009;</mml:i><mml:sub><mml:i><mml:i>&#x03C1;</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>&#x2009;</mml:i><mml:sub><mml:i><mml:i>&#x03C3;</mml:i></mml:i><mml:i><mml:i>i</mml:i></mml:i></mml:sub><mml:i>&#x2009;</mml:i><mml:sub><mml:i><mml:i>&#x03C3;</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 = [
('&ndash;','–'),
('&rsquo;','É'),
]

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
Not sure if this will help but you should try this directory compare prog in your problem.
Reputation Points: 6
Solved Threads: 0
Newbie Poster
Michael_Smith is offline Offline
3 posts
since Jan 2009
Feb 7th, 2009
0

Re: Search and replace multiple file

This post is over a year old. Just let it die.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Feb 8th, 2009
-1

Re: Search and replace multiple file

Quote ...
This post is over a year old. Just let it die.
Ah sorry, I just didn't notice the date. I just fixed the link for a case it is needed to him...
Not sure if this will help but you should try this soft for directory compare in your problem.
Last edited by Narue; Mar 10th, 2009 at 6:30 pm. Reason: snipped link
Reputation Points: 6
Solved Threads: 0
Newbie Poster
Michael_Smith is offline Offline
3 posts
since Jan 2009
Oct 7th, 2010
0

To search and replace text in multiple files

This code will search for the given text in all the files in directories and subdirectories recursively and replace with replace text in given directory:

Save this following peace of code to a search_and_replace_in_multiple_files.py and run.
search_and_replace_in_multiple_files.py "search_string" "replace_string" "c:\\"
it will search in entire c:\ drive all the files and where ever it finds the string it will replace.

Python Syntax (Toggle Plain Text)
  1. #!/usr/bin/env python
  2. # To search and replace a string in multiple files
  3.  
  4. import sys
  5. import os
  6.  
  7. if len(sys.argv) < 2:
  8. print 'usage: %s search_text replace_text directory' \
  9. % os.path.basename(sys.argv[0])
  10. sys.exit(0)
  11.  
  12.  
  13. stext = sys.argv[1]
  14. rtext = sys.argv[2]
  15. if len(sys.argv) == 4:
  16. path = os.path.join(sys.argv[3], '*')
  17. else:
  18. path = '*'
  19.  
  20.  
  21. print 'finding: %s and replacing with: %s' % (stext, rtext)
  22. ## Traversing through entire directory recursively.
  23. mydir= sys.argv[3]
  24. hole_list=[]
  25. def myfun(s1, s2, s3):
  26. for file in s3:
  27. if ".py" in file:
  28. full_path_file=s2+"\\"+file
  29. hole_list.append(full_path_file)
  30. os.path.walk(mydir, myfun, '')
  31.  
  32.  
  33. for file in hole_list:
  34. r = open(file, 'r')
  35. file_content=r.read()
  36. file_content=file_content.replace(stext,rtext)
  37. w = open(file, 'w')
  38. w.write(file_content)
  39.  
  40. #self.server.osget_path()
Last edited by seetharama Raju; Oct 7th, 2010 at 6:49 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
seetharama Raju is offline Offline
1 posts
since Oct 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Matrix Subtraction : Gives NotImplemented
Next Thread in Python Forum Timeline: Passing variables via url with slashes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC