Skip to main content

Be aware of the count 0 when using Pythons re sub to find and replace text


I have an xml file and I want to remove a whole section of element without actually parsing the XML file. I wrote an Python script to do just that. I first tested it out like this:

import re
input_text = open(my.xml).read()
result = re.findall(r<library-ref>.*</library-ref>, input_text, re.S)
print(result)


The result IS what I wanted, so I thought I can quickly replace it with something like this:

result = re.sub(r<library-ref>.*</library-ref>, , input_text, re.S)

And to my surprise this does not work. It took me a while to notice that I mis used the method according to the documentation, which has this signature:

re.sub(pattern, repl, string, count=0, flags=0)

At first glace, I thought that count is already default to zero and I do not need to set it, but because that is a positional parameter, you must include it! So you suppose to call it explicit like this:

result = re.sub(r<library-ref>.*</library-ref>, , input_text, 0, re.S)

Or, if you like, you can use the "flags" parameter name explicitly without the odd looking zero.

result = re.sub(r<library-ref>.*</library-ref>, , input_text, flags=re.S)

So here is an example of flexible dynamic typing of optional parameters can bite if you are not careful.

Popular posts from this blog

Baixar PS1 Crash Bandicoot 2 Cortex Strikes Back

PARTE 1: http://www.megaupload.com/?d=1AVO7X8T PARTE 2: http://www.megaupload.com/?d=WUBY6EAH

Barbarian Level 70 Paragon Level 191

Ive been thoroughly enjoying Reaper of Souls, so much so that Ive skipped posting here for some time (sorry, guys) and just concentrated on leveling-up my barbarian character. As you can see from the screenshot below, Ive been spending quite some time transmogrifying and buying up dyes making sure everythings coordinated and shit. Right now my barbarian is the most powerful (i.e., the one able to dish out the most damage) among my characters but I have a feeling that my crusader is going to overtake him really fast (at least until he levels up to 70 - just 2 more levels to go). Also, hes the sugar daddy of the bunch, the one tasked to make the most money for spending by the group. Ive given him some rest in the last couple of weeks however, preferring to adventure with my crusader and exploring his different abilities. Ill be getting him back on track soon enough though as the barbarian is my favorite class. Im currently playing at Torment II but I hope to progress to Torment III as so...

Baixar Filme Download Quadrilogia Veloses e Furiosos – Dublado

Sinopse : Eles t�m muita adrenalina e m�quinas poderosas, mas acima de tudo, eles possuem a extrema necessidade de velocidade. Nas ruas de Los Angeles, toda noite Dominic Toretto (Vin Diesel) transforma seus desafiantes em p�. Mas agora, h� um novo furor nas ruas. Eles sabem que ele � dur�o e r�pido, mas o que n�o sabem � que ele � um detetive determinado e veloz (Paul Walker). Com intensa a��o e velocidade, espantosas acrobacias e com o p� fundo no acelerador, este ataque furioso o colocar� no banco do motorista e o desafiar� a exceder os seus limites. Lan�amento : 2001 a 2008 Dura��o : 100 min apx. Cada Qualidade : DVDRip �udio : 10 V�deo : 10 Formato : RMVB Tamanho : 350 Mb apx. Cada �dioma : Portugu�s Veloses e Furiosos 1 >> Megaupload Veloses e Furiosos 2 >> Megaupload Veloses e Furiosos 3 >> Megaupload Veloses e Furiosos 4 >> Megaupload