Top-rated recipes tagged "text"http://code.activestate.com/recipes/tags/text/top/2013-06-21T14:47:21-07:00ActiveState Code RecipesHex dumper (Python)
2002-08-05T04:27:10-07:00Sébastien Keimhttp://code.activestate.com/recipes/users/131730/http://code.activestate.com/recipes/142812-hex-dumper/
<p style="color: grey">
Python
recipe 142812
by <a href="/recipes/users/131730/">Sébastien Keim</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>Hexadecimal display of a byte stream</p>
Using terminfo for portable color output & cursor control (Python)
2006-03-27T19:20:16-08:00Edward Loperhttp://code.activestate.com/recipes/users/2637812/http://code.activestate.com/recipes/475116-using-terminfo-for-portable-color-output-cursor-co/
<p style="color: grey">
Python
recipe 475116
by <a href="/recipes/users/2637812/">Edward Loper</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 3.
</p>
<p>The curses module defines several functions (based on terminfo) that can be used to perform lightweight cursor control & output formatting (color, bold, etc). These can be used without invoking curses mode (curses.initwin) or using any of the more heavy-weight curses functionality. This recipe defines a TerminalController class, which can make portable output formatting very simple. Formatting modes that are not supported by the terminal are simply omitted.</p>
Yet Another Python Templating Utility (YAPTU) (Python)
2001-08-31T15:17:32-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52305-yet-another-python-templating-utility-yaptu/
<p style="color: grey">
Python
recipe 52305
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 6.
</p>
<p>"Templating" (copying an input file to output, on the fly inserting Python expressions and statements) is a frequent need, and YAPTU is a small but complete Python module for that; expressions and statements are identified by arbitrary user-chosen regular-rexpressions.</p>
Pure Python PDF to text converter (Python)
2007-04-12T11:05:10-07:00Dirk Holtwickhttp://code.activestate.com/recipes/users/636691/http://code.activestate.com/recipes/511465-pure-python-pdf-to-text-converter/
<p style="color: grey">
Python
recipe 511465
by <a href="/recipes/users/636691/">Dirk Holtwick</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>This example shows how to extract text informations from a PDF file without the need of system dependent tools or code. Just use the pyPdf library from <a href="http://pybrary.net/pyPdf/" rel="nofollow">http://pybrary.net/pyPdf/</a></p>
Guaranteed conversion to unicode or byte string (Python)
2006-01-23T22:14:48-08:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/466341-guaranteed-conversion-to-unicode-or-byte-string/
<p style="color: grey">
Python
recipe 466341
by <a href="/recipes/users/2382677/">Wai Yip Tung</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>Python's built in function str() and unicode() return a string representation of the object in byte string and unicode string respectively. This enhanced version of str() and unicode() can be used as handy functions to convert between byte string and unicode. This is especially useful in debugging when mixup of the string types is suspected.</p>
Templite (Python)
2006-05-16T17:35:27-07:00tomer filibahttp://code.activestate.com/recipes/users/2520014/http://code.activestate.com/recipes/496702-templite/
<p style="color: grey">
Python
recipe 496702
by <a href="/recipes/users/2520014/">tomer filiba</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 3.
</p>
<p>A light-weight, fully functional, general purpose templating engine, in just 40 lines of code</p>
Chomsky random text generator (Python)
2005-09-13T18:46:01-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/440546-chomsky-random-text-generator/
<p style="color: grey">
Python
recipe 440546
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>Creates believable Chomsky style obfuscated prose.</p>
Easy to use, easy to read, python based HTML generation (Python)
2013-06-21T14:47:21-07:00Pavloshttp://code.activestate.com/recipes/users/4185038/http://code.activestate.com/recipes/578436-easy-to-use-easy-to-read-python-based-html-generat/
<p style="color: grey">
Python
recipe 578436
by <a href="/recipes/users/4185038/">Pavlos</a>
(<a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/template/">template</a>, <a href="/recipes/tags/text/">text</a>).
Revision 4.
</p>
<p>I was looking for a simple way to generate HTML directly in python that does not require learning a new template 'language' nor requires the installation of a big complex package. Closest thing I found was James Casbon's attempt(https://gist.github.com/1461441). This is my version of the same idea. </p>
<p>(2013-04-21) added some simplifications and support for switching off string interpolation. Added to github:</p>
<p><a href="https://github.com/pavlos-christoforou/web" rel="nofollow">https://github.com/pavlos-christoforou/web</a></p>
Render tables for text interface (Python)
2010-04-20T18:02:51-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577202-render-tables-for-text-interface/
<p style="color: grey">
Python
recipe 577202
by <a href="/recipes/users/57155/">Denis Barmenkov</a>
(<a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/table/">table</a>, <a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>Sometime pprint module is not enough for formatting data for console or log file output.
This module provide function which fill the gap.</p>
<p><strong>Sample function call:</strong></p>
<pre class="prettyprint"><code>nums = [ '1', '2', '3', '4' ]
speeds = [ '100', '10000', '1500', '12' ]
desc = [ '', 'label 1', 'none', 'very long description' ]
lines = format_table( [(nums, ALIGN_RIGHT|PADDING_ALL, 'NUM'),
(speeds, ALIGN_RIGHT|PADDING_ALL, 'SPEED'),
(desc, ALIGN_LEFT|PADDING_ALL, 'DESC')] )
</code></pre>
<p><strong>Output:</strong></p>
<pre class="prettyprint"><code>=======================================
| NUM | SPEED | DESC |
=======================================
| 1 | 100 | |
| 2 | 10000 | label 1 |
| 3 | 1500 | none |
| 4 | 12 | very long description |
=======================================
</code></pre>
Sorting big files the Python 2.6 way (Python)
2009-05-30T21:51:09-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576755-sorting-big-files-the-python-26-way/
<p style="color: grey">
Python
recipe 576755
by <a href="/recipes/users/924636/">Gabriel Genellina</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/text/">text</a>).
Revision 3.
</p>
<p>This is just a rewrite of <a href="http://code.activestate.com/recipes/466302/">Recipe 466302</a> "Sorting big files the Python 2.4 way", taking advantage of heapq.merge, context managers, and other niceties of newer Python versions. It can be used to sort very large files (millions of records) in Python. No record termination character is required, hence a record may contain embedded binary data, newlines, etc. You can specify how many temporary files to use and where they are located.</p>
Byte to Hex and Hex to Byte String Conversion (Python)
2007-03-21T06:35:40-07:00Simon Peveretthttp://code.activestate.com/recipes/users/1410328/http://code.activestate.com/recipes/510399-byte-to-hex-and-hex-to-byte-string-conversion/
<p style="color: grey">
Python
recipe 510399
by <a href="/recipes/users/1410328/">Simon Peverett</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>I write a lot of ad-hoc protocol analysers using Python. Generally, I'm dealing with a byte stream that I want to output as a string of hex. Sometimes, I want to convert it back again. Eventually, I got round to putting the functions in a module so I wouldn't keep cut and pasting them :)</p>
Simplified string substitution (Python)
2004-10-24T08:57:28-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/305306-simplified-string-substitution/
<p style="color: grey">
Python
recipe 305306
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 4.
</p>
<p>Replaces %(name)s style formatting with [name] style formatting. Custom open and close delimiters can be specified. Useful for templates exposed to end-users.</p>
Shell-like data processing (Python)
2004-04-06T15:06:40-07:00Maxim Krikunhttp://code.activestate.com/recipes/users/1085177/http://code.activestate.com/recipes/276960-shell-like-data-processing/
<p style="color: grey">
Python
recipe 276960
by <a href="/recipes/users/1085177/">Maxim Krikun</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>This module introduces an alternative syntax a-la shell pipes for sequence-oriented functions, such as filter, map, etc., via certain classes that override __ror__ method.</p>
Table indentation (Python)
2004-02-14T03:50:24-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/267662-table-indentation/
<p style="color: grey">
Python
recipe 267662
by <a href="/recipes/users/2591466/">George Sakkis</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 7.
</p>
<p>A function for pretty-printing a table.</p>
Single-pass Multiple Replace (Python)
2001-10-12T14:45:58-07:00Xavier Defranghttp://code.activestate.com/recipes/users/117591/http://code.activestate.com/recipes/81330-single-pass-multiple-replace/
<p style="color: grey">
Python
recipe 81330
by <a href="/recipes/users/117591/">Xavier Defrang</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 5.
</p>
<p>This recipe shows how to use the Python standard re module to perform single-pass multiple string substitution using a dictionary.</p>
The Matrix Effect (JavaScript)
2009-07-28T10:13:20-07:00Panda Moniomhttp://code.activestate.com/recipes/users/4171270/http://code.activestate.com/recipes/576861-the-matrix-effect/
<p style="color: grey">
JavaScript
recipe 576861
by <a href="/recipes/users/4171270/">Panda Moniom</a>
(<a href="/recipes/tags/dynamic_drive/">dynamic_drive</a>, <a href="/recipes/tags/effect/">effect</a>, <a href="/recipes/tags/javascript/">javascript</a>, <a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/text/">text</a>).
</p>
<p>This code is and opensource code from Dynamic Drive.</p>
<p>Add the Matrix effect to any text. I have displayed the code for and entire demo page. Any text that you want to have the Matrix effect, put it inside <div id="matrix> and </div>.</p>
Python word frequency count using sets and lists (Python)
2009-03-26T23:00:54-07:00nickhttp://code.activestate.com/recipes/users/4169647/http://code.activestate.com/recipes/576699-python-word-frequency-count-using-sets-and-lists/
<p style="color: grey">
Python
recipe 576699
by <a href="/recipes/users/4169647/">nick</a>
(<a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/unique_words/">unique_words</a>, <a href="/recipes/tags/word_frequency/">word_frequency</a>).
</p>
<p>This lists unique words and word frequencies occurring in a Python string. You can ignore or take account of letter case in distinguishing words, and you can pass it your own inclusion list of characters allowed in words (e.g. is "import123" the kind of word you want to list, or not? It might be if you're a programmer.) By default only alpha chars are allowed in words.</p>
<p>At first glance having the whole piece of text, and intermediate results, in memory at once is a problem for large files. But it's zippy: it found 1600 unique words in a 7M SQL script (472,000 words in original) in 20 seconds, and hardly notices a 4000-word document cut and pasted across from a word processor.</p>
<p>With a bit of extra work, the algorithm could be fed a very large file in chunks. Anyone?</p>
Templite+ (Python)
2009-02-28T04:44:46-08:00Thimo Kraemerhttp://code.activestate.com/recipes/users/4169283/http://code.activestate.com/recipes/576663-templite/
<p style="color: grey">
Python
recipe 576663
by <a href="/recipes/users/4169283/">Thimo Kraemer</a>
(<a href="/recipes/tags/template/">template</a>, <a href="/recipes/tags/templating/">templating</a>, <a href="/recipes/tags/text/">text</a>).
Revision 13.
</p>
<p>Revised version of <a href="http://code.activestate.com/recipes/496702/">Templite</a>, a light-weight, fully functional, general purpose templating engine</p>
Simplify printing using the string template feature added in 2.4 (Python)
2008-02-23T08:36:18-08:00Tim Keatinghttp://code.activestate.com/recipes/users/2246480/http://code.activestate.com/recipes/543272-simplify-printing-using-the-string-template-featur/
<p style="color: grey">
Python
recipe 543272
by <a href="/recipes/users/2246480/">Tim Keating</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 3.
</p>
<p>The alternate substitution delimiter introduced in 2.4 (string.Template) was supposed to make string substitution easier. In fact it is a little cumbersome to use. This recipe employs a little stack hackery to make it as easy as it ought to be.</p>
QuackTemplate.Wrapper (Python)
2006-05-22T09:21:16-07:00Costas Malamashttp://code.activestate.com/recipes/users/2897303/http://code.activestate.com/recipes/496730-quacktemplatewrapper/
<p style="color: grey">
Python
recipe 496730
by <a href="/recipes/users/2897303/">Costas Malamas</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>QuackTemplate.Wrapper is a template helper class: it takes arbitrary Python objects and uses__gettitem__ so that the wrapped object can be passed into the standard Python string substitution mechanism. It can follow method calls, dict keys and list members (to some extent).</p>