The closest equivalent to tcl string map will be str.translate , but unfortunately it can only display single characters. Therefore, to obtain a similar compact example, you must use a regular expression. This can be done using the look-behind / look-ahead statements , but first you need to replace \r :
import re oldtext = """\ This would keep paragraphs separated. This would keep paragraphs separated. This would keep paragraphs separated. \tThis would keep paragraphs separated. \rWhen, in the course of human events, it becomes necessary \rfor one people """ newtext = re.sub(r'(?<!\n)\n(?![\n\t])', ' ', oldtext.replace('\r', ''))
exit:
This would keep paragraphs separated. This would keep paragraphs separated. This would keep paragraphs separated. This would keep paragraphs separated. When, in the course of human events, it becomes necessary for one people
I doubt it is as efficient as tcl code.
UPDATE
I did a little test using this Project Gutenberg EBook of War and Peace (Plain Text UTF-8, 3.1 MB). Here is my tcl script:
set fp [open "gutenberg.txt" r] set oldtext [read $fp] close $fp set newtext [string map "{\r} {} {\n\n} {\n\n} {\n\t} {\n\t} {\n} { }" $oldtext] puts $newtext
and my python equivalent:
import re with open('gutenberg.txt') as stream: oldtext = stream.read() newtext = re.sub(r'(?<!\n)\n(?![\n\t])', ' ', oldtext.replace('\r', '')) print(newtext)
Crude oil performance test:
$ /usr/bin/time -f '%E' tclsh gutenberg.tcl > output1.txt 0:00.18 $ /usr/bin/time -f '%E' python gutenberg.py > output2.txt 0:00.30
So, as expected, the tcl version is more efficient. However, exiting the python version looks a bit cleaner (no extra spaces are added at the beginning of lines).