Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

common python snippets



Python Modules  http://wiki.python.org/moin/UsefulModules


regex subsitution in myString
myString= re.sub("\([0-9][0-9]*\)", "", myString)
myString= re.sub("\\\\", "", myString)      # remove a single backslash
myString= re.sub(r"\\", "", myString)      # remove a single backslash using a "raw string"


string substitution (*not* for regex)
    c = c.replace(u'\xae', '') # ®
    c = c.replace(u'\xbb', '') # »
    c = c.replace(u'\x99', '') # char for TM
    c = c.replace(u'\xa9', '') # ©
    c = c.replace(u'—', '-')  # replace mdash u2014 with regular dash
    c = c.replace('?', '') # ?
    c = c.replace(':', '') # :
    c = c.replace(';', '') # ;




save regex-matching string to a new variable
        pageObj = re.search("od [0-9][0-9]*", pagetext)
        if pageObj:
            pageObj2 = re.search("[0-9][0-9]*", pageObj.group())
            numb = pageObj2.group()
            print "numb is ",numb
            pages = range(2,int(numb)+1)

            for p in pages:
                newUrl = locUrl+'afpg/'+str(p)+'/Default.aspx'
                print "adding ", newUrl

Starting Off in Python


We’ll start off by looking at material from these sites.
http://www.astro.ufl.edu/~warner/prog/python.html
http://www.sthurlow.com/python/

http://learnpythonthehardway.org/book/ <- recommended for new programmers