Python for beginners/anthology: Difference between revisions
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...") |
|||
Line 9: | Line 9: | ||
with open('peter_rabbit.txt') as f: | with open('peter_rabbit.txt') as f: | ||
− | + | for line in f: | |
print line | print line | ||
Line 20: | Line 20: | ||
with open('peter_rabbit.txt') as f: | 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)) | print(“ “.join(new_text)) | ||
Line 37: | Line 37: | ||
def remove_punct(f): | def remove_punct(f): | ||
− | + | tokens = (' '.join(line.replace('\n', '') for line in f)).lower() | |
− | + | for c in string.punctuation: | |
tokens= tokens.replace(c," ") | tokens= tokens.replace(c," ") |
Revision as of 15:35, 21 November 2015
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