Actions

Difference between revisions of "Python for beginners/anthology"

From Algolit

(Created page with " '''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('p...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 +
Other pages: [[Python_for_beginners|Python for Beginners]] // [[Python for beginners/some vocabulary|Some vocabulary]] // [[Python_for_beginners/loops_and_conditions|Loops and Conditions]]
  
 
'''READING AND (RE)WRITING TEXTS'''
 
'''READING AND (RE)WRITING TEXTS'''
Line 9: Line 10:
 
with open('peter_rabbit.txt') as f:  
 
with open('peter_rabbit.txt') as f:  
  
    for line in f:  
+
for line in f:  
  
        print line  
+
print line  
  
  
Line 20: Line 21:
 
with open('peter_rabbit.txt') as f:  
 
with open('peter_rabbit.txt') as f:  
  
    for line in f:  
+
for line in f:  
  
for word in line:
+
for word in line:
  
if len(word) >= 5:
+
if len(word) >= 5:
  
newtext.append(word)
+
newtext.append(word)
  
 
print(“ “.join(new_text))  
 
print(“ “.join(new_text))  
Line 57: Line 58:
 
def select(names):  
 
def select(names):  
  
    name = choice(names)  
+
name = choice(names)  
  
    return name  
+
return name  
  
  

Latest revision as of 15:38, 21 November 2015

Other pages: Python for Beginners // Some vocabulary // Loops and Conditions

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