Popular recipes tagged "meta:requires=fileinput"http://code.activestate.com/recipes/tags/meta:requires=fileinput/2016-11-07T20:28:01-08:00ActiveState Code RecipesBatch conversion of text files to PDF with fileinput and xtopdf (Python) 2016-11-07T20:28:01-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580715-batch-conversion-of-text-files-to-pdf-with-fileinp/ <p style="color: grey"> Python recipe 580715 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/batch/">batch</a>, <a href="/recipes/tags/batchmode/">batchmode</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/text_processing/">text_processing</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to do a batch conversion of the content of multiple text files into a single PDF file, with a) an automatic page break after the content of each text file (in the PDF output), b) page numbering, and c) a header and footer on each page.</p> <p>It uses the fileinput module (part of the Python standard library), and xtopdf, a Python library for conversion of other formats to PDF.</p> <p>xtopdf is available here: <a href="https://bitbucket.org/vasudevram/xtopdf" rel="nofollow">https://bitbucket.org/vasudevram/xtopdf</a></p> <p>and a guide to installing and using xtopdf is here:</p> <p><a href="http://jugad2.blogspot.in/2012/07/guide-to-installing-and-using-xtopdf.html" rel="nofollow">http://jugad2.blogspot.in/2012/07/guide-to-installing-and-using-xtopdf.html</a></p> <p>Here is a sample run of the program:</p> <p>python BTTP123.pdf text1.txt text2.txt text3.txt</p> <p>This will read the content from the three text files specified and write it into the PDF file specified, neatly formatted.</p> Benford's Law demo (Python) 2010-10-19T10:56:51-07:00Glenn Hutchingshttp://code.activestate.com/recipes/users/4175415/http://code.activestate.com/recipes/577431-benfords-law-demo/ <p style="color: grey"> Python recipe 577431 by <a href="/recipes/users/4175415/">Glenn Hutchings</a> (<a href="/recipes/tags/benford/">benford</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/plotting/">plotting</a>). </p> <p>Here's a simple program to demonstrate <a href="http://en.wikipedia.org/wiki/Benford%27s_law">Benford's Law</a>, which also shows the simple power of <a href="http://matplotlib.sourceforge.net">matplotlib</a>. It reads from a bunch of files (or stdin, if none specified), extracts the leading digits of all number-like strings found, and plots the distribution in a window together with the expected result if Benford's law applies.</p> expand_tabs.py - expand tabs in files in-place (Python) 2006-10-07T20:55:12-07:00Ori Peleghttp://code.activestate.com/recipes/users/2056315/http://code.activestate.com/recipes/498179-expand_tabspy-expand-tabs-in-files-in-place/ <p style="color: grey"> Python recipe 498179 by <a href="/recipes/users/2056315/">Ori Peleg</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 2. </p> <p>expand_tabs.py - Similar to Unix's expand(1) command, but can edit the files in-place.</p> AWK like text processing (Python) 2006-03-22T19:06:35-08:00Ian Bygravehttp://code.activestate.com/recipes/users/2823999/http://code.activestate.com/recipes/475163-awk-like-text-processing/ <p style="color: grey"> Python recipe 475163 by <a href="/recipes/users/2823999/">Ian Bygrave</a> (<a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>AWK is a text processing language that makes it easy to "search files for lines [...] that contain certain patterns. When a line matches one of the patterns, awk performs specified actions on that line." (GNU Awk User's Guide) This recipe provides a way to do the same thing in python.</p> Echo input with a time delay after each line (Python) 2005-11-02T10:07:35-08:00Edi Hancehttp://code.activestate.com/recipes/users/2650490/http://code.activestate.com/recipes/442518-echo-input-with-a-time-delay-after-each-line/ <p style="color: grey"> Python recipe 442518 by <a href="/recipes/users/2650490/">Edi Hance</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>This script echos the input with a time delay after each line. The input can be from standard input or a list of files on the command line, like the Unix 'cat' command. The delay between lines is adjustable, and defaults to 1 second.</p> Sort a file (Python) 2005-09-28T15:34:55-07:00Phil Robarehttp://code.activestate.com/recipes/users/2608288/http://code.activestate.com/recipes/440612-sort-a-file/ <p style="color: grey"> Python recipe 440612 by <a href="/recipes/users/2608288/">Phil Robare</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>A simple (4 lines) recipe that will sort a file named on the command line and write the sorted lines to standard output.</p> sed/awk : Python script to rename subdirectories of a directory tree, replace strings in files (Python) 2005-07-05T12:33:51-07:00Bibha Tripathihttp://code.activestate.com/recipes/users/2437311/http://code.activestate.com/recipes/435904-sedawk-python-script-to-rename-subdirectories-of-a/ <p style="color: grey"> Python recipe 435904 by <a href="/recipes/users/2437311/">Bibha Tripathi</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>I needed to write a sed/awk Python equivalent for walking into a directory tree and renaming certain subdirectories, while also looking into all xml files on the way and replacing/modifying certain strings in those files.</p> <p>It would be nicer if someone could suggest an enhanced re.sub(regex, replacement, subject) where I could replace all strings of a certain pattern with other strings of a certain pattern i.e. the second argument in re.sub namely 'replacement' would then be a regular expression and would be a different string for each different string in 'subject' that matches with the pattern 'regex'. For example 'arthinternational-d' would be replaced by 'arthinternational-r', 'arthfmt-d' would be replaced by 'arthfmt-r' but 'a-d' would remain unmodified.</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> foreach.py - Execute a python function for a selected set of files. (Python) 2004-08-19T17:12:33-07:00lonetwinhttp://code.activestate.com/recipes/users/1951998/http://code.activestate.com/recipes/300411-foreachpy-execute-a-python-function-for-a-selected/ <p style="color: grey"> Python recipe 300411 by <a href="/recipes/users/1951998/">lonetwin</a> . Revision 3. </p> <p>equivalent to doing $ find -name "*ext" -exec (some python function) {} ';'</p> Indexing into XML (Python) 2002-10-08T05:38:27-07:00Dave Pawsonhttp://code.activestate.com/recipes/users/127209/http://code.activestate.com/recipes/155582-indexing-into-xml/ <p style="color: grey"> Python recipe 155582 by <a href="/recipes/users/127209/">Dave Pawson</a> . </p> <p>Build an index into a directory of xml files. Search the index. Based around the indexing code from Alex Martelli, linux magazine, July 2002.</p> Replace occurrence off string in files under directory tree (Python) 2002-04-18T12:37:37-07:00Ivan Brkanachttp://code.activestate.com/recipes/users/352905/http://code.activestate.com/recipes/120991-replace-occurrence-off-string-in-files-under-direc/ <p style="color: grey"> Python recipe 120991 by <a href="/recipes/users/352905/">Ivan Brkanac</a> (<a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>This script go thru directory tree and looks for files off specified pattern, and then replace in those files string with new one, overwriting old file without backups</p>