Most viewed recipes tagged "text"http://code.activestate.com/recipes/tags/text/views/2013-07-27T09:44:06-07:00ActiveState Code RecipesByte 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> Converting between ASCII numbers and characters (Python) 2001-06-10T15:39:16-07:00Hamish Lawsonhttp://code.activestate.com/recipes/users/98049/http://code.activestate.com/recipes/65117-converting-between-ascii-numbers-and-characters/ <p style="color: grey"> Python recipe 65117 by <a href="/recipes/users/98049/">Hamish Lawson</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>You want to get the ASCII number of a character, or you want to get the character given by an ASCII number.</p> Convert a string into a raw string (Python) 2001-06-19T00:18:59-07:00Brett Cannonhttp://code.activestate.com/recipes/users/98030/http://code.activestate.com/recipes/65211-convert-a-string-into-a-raw-string/ <p style="color: grey"> Python recipe 65211 by <a href="/recipes/users/98030/">Brett Cannon</a> (<a href="/recipes/tags/text/">text</a>). Revision 3. </p> <p>This function takes in an arbitrary string and converts it into its raw string equivalent. Unfortunately \x will raise a ValueError and I cannot figure out how to deal with it.</p> <p>[2001-06-18: Completely reworked function for performance]</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> Checking whether a string contains a set of chars (Python) 2001-07-03T21:00:41-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/65441-checking-whether-a-string-contains-a-set-of-chars/ <p style="color: grey"> Python recipe 65441 by <a href="/recipes/users/98061/">Jürgen Hermann</a> (<a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>While the find() and count() string functions can check for string occurences, there is no function to check on the occurence of a set of characters.</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> Search and replace text in a file. (Python) 2001-03-13T20:18:13-08:00Jeff Bauerhttp://code.activestate.com/recipes/users/98055/http://code.activestate.com/recipes/52250-search-and-replace-text-in-a-file/ <p style="color: grey"> Python recipe 52250 by <a href="/recipes/users/98055/">Jeff Bauer</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>A simple text search and replace program.</p> Replacing a portion of a string (Python) 2001-06-21T09:35:10-07:00Hamish Lawsonhttp://code.activestate.com/recipes/users/98049/http://code.activestate.com/recipes/59890-replacing-a-portion-of-a-string/ <p style="color: grey"> Python recipe 59890 by <a href="/recipes/users/98049/">Hamish Lawson</a> (<a href="/recipes/tags/text/">text</a>). Revision 3. </p> <p>You want to replace that portion of a string at a given position.</p> Find and replace string in all files in a directory (Python) 2004-04-12T05:56:11-07:00Anthony Barkerhttp://code.activestate.com/recipes/users/122940/http://code.activestate.com/recipes/277753-find-and-replace-string-in-all-files-in-a-director/ <p style="color: grey"> Python recipe 277753 by <a href="/recipes/users/122940/">Anthony Barker</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>A friend of mine needed to change the IP address in his DNS hosting bind configuration. Here is a quick script I wrote during lunch it interates all files in a directory and finds and replaces as string. In this case an IP address.</p> Find All Indices of a SubString in a Given String (Python) 2006-12-14T10:08:37-08:00Bibha Tripathihttp://code.activestate.com/recipes/users/2437311/http://code.activestate.com/recipes/499314-find-all-indices-of-a-substring-in-a-given-string/ <p style="color: grey"> Python recipe 499314 by <a href="/recipes/users/2437311/">Bibha Tripathi</a> (<a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>I needed a version of the string.index(sub) function which returns a list of indices of ALL occurances of a substring in the string.</p> <p>Is there a better/shorter/more efficient way to do this? Please share.</p> Droids ( a text adventure ) (Python) 2013-07-27T09:44:06-07:00jayhttp://code.activestate.com/recipes/users/4186699/http://code.activestate.com/recipes/578618-droids-a-text-adventure/ <p style="color: grey"> Python recipe 578618 by <a href="/recipes/users/4186699/">jay</a> (<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/text/">text</a>). </p> <p>for beginners.</p> Hamming distance (Python) 2006-12-11T10:22:15-08:00Michael Mayhewhttp://code.activestate.com/recipes/users/4018705/http://code.activestate.com/recipes/499304-hamming-distance/ <p style="color: grey"> Python recipe 499304 by <a href="/recipes/users/4018705/">Michael Mayhew</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>Was doing some work with strings and threw this together. This will calculate the Hamming distance (or number of differences) between two strings of the same length.</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> 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> Add thousands separator commas to formatted numbers (Python) 2006-10-07T15:49:28-07:00Manuel Garciahttp://code.activestate.com/recipes/users/2827266/http://code.activestate.com/recipes/498181-add-thousands-separator-commas-to-formatted-number/ <p style="color: grey"> Python recipe 498181 by <a href="/recipes/users/2827266/">Manuel Garcia</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>Needed formatted numbers with thousands separator commas added on an end-user report. The usual way mentioned is to use 'module locale.format', but that didn't work cleanly on my Windows machine, and the cure seemed worse than the disease.</p> Split String into n-size pieces (Python) 2006-06-06T12:24:23-07:00Matt Wallacehttp://code.activestate.com/recipes/users/2913776/http://code.activestate.com/recipes/496784-split-string-into-n-size-pieces/ <p style="color: grey"> Python recipe 496784 by <a href="/recipes/users/2913776/">Matt Wallace</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>The code below takes a string and returns a list containing the n-sized pieces of the string. For example: splitCount('aabbccdd', 2) =&gt; ['aa', 'bb', 'cc', 'dd']</p> Python text to pdf converter (Python) 2004-02-25T11:45:48-08:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/189858-python-text-to-pdf-converter/ <p style="color: grey"> Python recipe 189858 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/text/">text</a>). Revision 8. </p> <p>Python port of a very popular implementation of text2pdf in the C programming language. I ported this to python because it could be used as a script instead of executable which is the case with the original program. Most of the options remain the same.</p> Simulating the ternary operator in Python (Python) 2001-03-19T13:06:05-08:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/52282-simulating-the-ternary-operator-in-python/ <p style="color: grey"> Python recipe 52282 by <a href="/recipes/users/98061/">Jürgen Hermann</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>People coming from C, C++ or Perl might miss the so-called ternary operator ?: (condition ? then-expr : else-expr). It's most often used for avoiding several lines of code and temporary variables for very simple decisions, like printing the plural form of words after a counter (see example code).</p> <p>There are two ways to get the same effect in Python: selecting one of two values from a tuple, or using the special behaviour of the "and" and "or" operators in Python. The second method has the advantage that only ONE of the two possible expressions is evaluated, and is thus more close to the behaviour of ?: as defined by C.</p> Check that a string represents an integer number (Python) 2004-09-05T06:50:04-07:00Francesco Ricciardihttp://code.activestate.com/recipes/users/2020988/http://code.activestate.com/recipes/303495-check-that-a-string-represents-an-integer-number/ <p style="color: grey"> Python recipe 303495 by <a href="/recipes/users/2020988/">Francesco Ricciardi</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>Many times I needed to check if a string represented a number or not. This is a very short recipe that uses Python int() function to do the check, instead of looping on the single characters.</p> Using re.match, re.search, and re.group in if ... elif ... elif ... else ... (Python) 2005-11-16T12:03:18-08:00Peter Kleiweghttp://code.activestate.com/recipes/users/2005873/http://code.activestate.com/recipes/456151-using-rematch-research-and-regroup-in-if-elif-elif/ <p style="color: grey"> Python recipe 456151 by <a href="/recipes/users/2005873/">Peter Kleiweg</a> (<a href="/recipes/tags/text/">text</a>). Revision 5. </p> <p>A wrapper class for (a small part of) the 're' module, that enables you to do re.match() or re.search() in an 'if' test or 'elif' test and use the result of the match after the test.</p>