Actions

Python for beginners/anthology

From Algolit

Revision as of 15:35, 21 November 2015 by An (talk | contribs)

READING AND (RE)WRITING TEXTS


* you can print any text file

Note: in this case the text is in the same folder as the script, otherwise adapt path

with open('peter_rabbit.txt') as f:

       for line in f: 
       print line 


* now you can call operations on this text

new_text = []

with open('peter_rabbit.txt') as f:

       for line in f: 
               for word in line:
                       if len(word) >= 5:
                               newtext.append(word)

print(“ “.join(new_text))


* remove punctuation

import string

def remove_punct(f):

       tokens = (' '.join(line.replace('\n', ) for line in f)).lower()
       for c in string.punctuation:

tokens= tokens.replace(c," ")

return tokens

tokens = remove_punct(f)


* random choice & shuffle

from random import choice, shuffle

names = [peter, benjamin, flopsy, tod, tom, samuel, pie, ginger, moppet, nutkin, timmy, tailor, johnny, mice, tittlemouse, tiggy, rabbit, jemima, jeremy, robinson, pigling]


def select(names):

   name = choice(names) 
   return name 


mix = shuffle(names)


* proposal to publish an anthology as a pdf

    • play with search for words in texts with certain suffixes from texts, remove letters
    • write new text to file
    • once all files are finished - transform & publish!

$ cat partfilename* > outputfilename

$ pandoc input.txt -o output.pdf