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

Beini WifiWay BackTrack Alternative Yes Xiaopan OS Its Better!

Beini is outdated, unsupported and lets face it super buggy. I managed to find a TinyCore Linux (TCL) that has been built similar to Beini. It boots much faster and has a few benefits over Beini. Check the video out below to see a successful Reaver Inflator run. The developer djyuzi at anywlan is active and pushing updates through at warp speed. Features � Reaver Inflator �   New GUI �   Higher Screen Resolution �   Connect to Wifi Networks �   Notepad �   XFE File Manager Updated �   Kernel 3.0.21 �   FeedingBottle 3.2.3 XiaoPanOS 0.4.2 � Update Tinycore 4.5 core  � Update minidwep-gtk-30419  � Update compat driver, update the firmware (including RTL8192SU)  � Added r8168, r8169 drivers  � Updated BCM43xx official drivers  � Updated the RTL8187L official driver (does not support the 8187B)  � You can run / home/tc/rtl8187B.sh compat driver on boot through XFE to load the rtl8187B manually Reaver Inflator is working on my ALFA RTL8188RU Device in Parallels also works for the RTL8187L S

Bash and Python scripts to unzip and modify an OpenOffice odt document

.odt files are actually containers (you can see one using unzip -l document.odt ). Within the container, content is in the content.xml file. Script info source Heres what Ive figured out about opening, modifying, and saving the content of an .odt file: To open the container for editing: # bash $unzip path/container.odt content.xml $unzip path/container.odt content.xml -d working/dir/path/ # -d places content.xml in a different directory. # Creates a content.xml file where you want it. # python >>>import zipfile >>>odt_file = zipfile.ZipFile(path/to/file.odt,a) >>> # Options are read only, write only, and append to existing >>>raw_xml = odt_file.read(content.xml) >>> # Reads content.xml in as a string, doesnt place a file. Modify the content.xml file by hand, or using a script, or using Python. >>> # Tip for using python: ElementTree is good at XML, and it can parse a file, but it cannot parse a string! >>> # So heres h

Beelzebub English Sub Batch 720p Download Free

    English: Beelzebub Japanese: ?????  Type: TV Episodes: 60 Score: 8.04 Aired: Jan 9, 2011 to Mar 25, 2012 Premiered: Winter 2011 Studios: Pierrot Plus Source: Manga Genres: Action, Comedy, Demons, Supernatural, School, Shounen Duration: 24 min. per ep. Rating: PG-13 - Teens 13 or older   ======================================= Type : MKV Audio : Japanese Subs : English [HorribleSubs] ======================================= Beelzebub English Sub (Batch) Mega.nz Link 720p MKV: Batch (Mega): Download (60 Episodes) To download batch from Mega.nz Just Select all files in the folder and select download as ZIP