Using .split to convert a bunch of items to an array (list)

Thanks to Zarino Zappio for this tip following an email to the Scraperwiki mailing list. The particular problem for which this was one possible solution was having a list of codes that you need a scraper to loop through. (In the end I solved this by using the =JOIN formula in the spreadsheet containing the codes (and adding an extra inverted comma at the start and finish))

But if that list of numbers is on a page, here's a useful way to convert it into an array:

"Get your scraper to loop over them as an array. You'd probably need to do some formatting, but it depends how your spreadsheet program handles copying cells. If it's generous and just gives you a string with the cell values separated by spaces or line breaks, you can use python to split the string into an array, over which you can loop…

pastedtext = "5237521 4398721 5293752 5967124"
lovelyarray = pastedtext.split()
# we now have a lovely array like ['5237521', '4398721', … '5967124']
for id in lovelyarray:
    print scraperwiki.scrape('http://yoururl.org.uk/page?iSchoolID=' + id)

Python lists

#Lists are useful as a way of assigning a series of values to a particular list name
#Those values can then be called by their position (index) in the series

myfridge = ['cheese', 'milk', 'beer']

#the square brackets indicate that this is a list
#each item in the list is separated by a comma
#items can then be called as follows:

myfridge[0]

#this would call the first item in the list (index position 0)

#you can also change the contents of a list with various functions as follows

myfridge.append('ham')

#this appends - .append - the string 'ham' to the list myfridge. 
#Note the full stop before .append and the parenthesis containing the argument to be passed
#the parenthesis could also use a variable instead of a string, or a function that returns a value, or another list

myfridge.pop(-1)

#this returns the last item in the list (at index position -1) and removes it from the list at the same time (pops)

#other functions include 
#counting how many times something occurs in a list - .count('ham')
#returning the index of a particular item in a list - .index('ham')
#removing the first instance in a list of a particular item - .remove('ham')
#inserting an item in a list at a particular position - .insert(3, 'ham')
#sorting items - .sort()
#reversing the order of items - .reverse()

#other list functions can be found at http://docs.python.org/tutorial/datastructures.html