Albert’s Python Cookbook

Write a csv file

import csv

with open('YOUR FILE NAME.csv', "wb") as csvFile:
        writer = csv.writer(csvFile, delimiter=',')
        for line in dataSET:

Find something in a text string:

someArray = ["Institute", "for", "Digital", "Research", "and", "Education"]

for object in someArray:
    if "it" in object:
        print "these objects have the letters 'it': "+object

    if "it" not in object:
        print "these objects do not have the letters 'it': "+object

Add arguments to python in command line:

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

Read CSV from command line argument:

#open the csv file    
f = open("somedata.csv")

#define csv variable
csv_f = csv.reader(f)

for row in csv_f:
    #do something with each row
    print row


import sys, unittest, time, re, selenium
class Sel(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        print "waiting"
        self.base_url = ""
        self.verificationErrors = []
        print "going"
        self.accept_next_alert = True
    def test_sel(self):
        driver = self.driver
        delay = 3
        driver.get(self.base_url + "/search?q=stckoverflow&src=typd")
        driver.find_element_by_link_text("View all").click()
        for i in range(1,5):
            self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        html_source = driver.page_source
        data = html_source.encode('utf-8')
        print data
if __name__ == "__main__":

Scrapy Tutorial


Back to workshop