Skip to content

Instantly share code, notes, and snippets.

@dketov
Created December 14, 2011 15:40
Show Gist options
  • Save dketov/1477068 to your computer and use it in GitHub Desktop.
Save dketov/1477068 to your computer and use it in GitHub Desktop.
Обработка текста
# -*- encoding: utf-8 -*-
"""
Сравнение текстов
"""
from difflib import get_close_matches
get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
import keyword
get_close_matches('wheel', keyword.kwlist)
get_close_matches('apple', keyword.kwlist)
get_close_matches('accept', keyword.kwlist)
text1 = """
Lavender's blue,
Diddle diddle,
Lavender's green,
When I am king,
Diddle diddle,
You shall be queen.
"""
text2 = u"""
Lavender's blue,
Lavender's green,
When I am king,
You shall be queen.
"""
import difflib
for line in difflib.context_diff(text1, text2, fromfile='text1', tofile='text2'):
sys.stdout.write(line)
# -*- encoding: utf-8 -*-
"""
Регулярные выражения, поиск
"""
import re
line = "Cats are smarter than dogs";
matchObj = re.match( r'(.*) are(\.*)', line, re.M|re.I)
if matchObj:
print "matchObj.group() : ", matchObj.group()
print "matchObj.group(1) : ", matchObj.group(1)
print "matchObj.group(2) : ", matchObj.group(2)
else:
print "No match!!"
line = "Cats are smarter than dogs";
matchObj = re.match( r'dogs', line, re.M|re.I)
if matchObj:
print "match --> matchObj.group() : ", matchObj.group()
else:
print "No match!!"
matchObj = re.search( r'dogs', line, re.M|re.I)
if matchObj:
print "search --> matchObj.group() : ", matchObj.group()
else:
print "No match!!"
# -*- encoding: utf-8 -*-
"""
Регулярные выражения, подстановка
"""
import re
phone = "2004-959-559 #This is Phone Number"
print phone
# Delete Python-style comments
num = re.sub(r'#.*$', "", phone)
print "Phone Num : ", num
# Remove anything other than digits
num = re.sub(r'\D', "", phone)
print "Phone Num : ", num
# -*- encoding: utf-8 -*-
"""
Формат PDF: библиотека reportlab
"""
from reportlab.pdfgen import canvas
def hello(c):
c.drawString(100,100,"Hello World")
c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()
c = canvas.Canvas("hello.pdf")
def alpha(canvas):
from reportlab.graphics.shapes import Rect
from reportlab.lib.colors import Color, black, blue, red
red50transparent = Color( 100, 0, 0, alpha=0.5)
c = canvas
c.setFillColor(black)
c.setFont('Helvetica', 10)
c.drawString(25,180, 'solid')
c.setFillColor(blue)
c.rect(25,25,100,100, fill=True, stroke=False)
c.setFillColor(red)
c.rect(100,75,100,100, fill=True, stroke=False)
c.setFillColor(black)
c.drawString(225,180, 'transparent')
c.setFillColor(blue)
c.rect(225,25,100,100, fill=True, stroke=False)
c.setFillColor(red50transparent)
c.rect(300,75,100,100, fill=True, stroke=False)
alpha(c)
c.showPage()
c.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment