regex - Regular expressions in a Python find-and-replace script? Update -


I'm new to Python scripting, please forgive me already if the answer to this question is naturally obvious .

I am trying to put together a largely search and replace script with Python. I am using a code similar to the following:

  infile = sys.argv [1] charenc = sys.argv [2] outFile = infile + 'output' findreplace = [('term1 ',' Term2 '),] INF = Open (Infel,' rb ') s = Unicode (inF.read), Charanak) inF.close () to find in pairs: outtext = s.replace (couple [0], pair [1]) S = Outtext Out F = Open (Outfile, 'WB') Out F.Lelect (Outtext.Incode ('UTF-8')) Out F. Close ()  

How do I go about finding and replacing the script for regular searches?

Specifically, I want to find some information (metadata) specified at the top of a text file. Example:

Title: This title is the author: It is the date of the author: this date is

and it is converting it into Latex format. Example: This is the date

Maybe I'm dealing with it incorrectly if it's a better way than regular expressions, please let me know!

Thank you!

Update: Thank you for posting some example codes in your reply! I can get it to work for so long till I take the place of the rapperplay action, but I can not get both to work. Now the problem is that I can not integrate it properly into that code Which I have found. How do I tell the script about multiple actions on 'Outtext' in the snippet given below?

  to search something: outtext = s.replace (couple [0], couple [1]) s = outtext  

< Div class = "post-text" itemprop = "text">

  & gt; & Gt; & Gt; Import re & gt; & Gt; & Gt; S = "" "Title: This is the title ... Author: This is the author ... Date: This date is" "" & gt; & Gt; & Gt; P = re.compile (r '^ (\ w +): \ s * (. +) $', Re.M) & gt; & Gt; & Gt; Print p.sub (r '\\\ 1 {\ 2}', s) \ title {this is title} \ author {it is authored} \ date {this is date}  

Use the function as a parameter to replace the case:

  def repl_cb (m): return "\\% s {% s}"% (m.group ( 1) .Lover (), M.G.P (2)) P = Rie Compel (R '^ (\ w +): \ s * (. +) $', Again. M) Print p. Sub (repl_cb, s)  

\ title {This is the title}
\ author {This is the author}
\ date {This is the date}


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -