The following is the same content that I have posted in my other blog on this topic in R, but written in Python. While I actually first wrote the code for doing this in Python, I’ll be posting the similar verbiage from that blog here.
Often when scraping data, websites will ask a user to enter a postal code to get the locations near it. If you are interested in collecting data on locations in Canada for an entire Province or the entire province from a site, it might be hard to find a list of all postal codes or FSAs in Canada or in a given province which is easy to use. Information on how FSAs work can be found here.
In this blog, I’m going to share a brief snippet of code that you can use to generate Canadian FSAs. While some FSAs generated may not actually exist, if we follow the rules about Canadian postal codes, it serves as a good substitute in lieu of an actual list.
is essentially 3 nested for-loops. While many programmers would not advise writing nested for-loops, I find that for this case it is easier to understand and write.
import string fsa_list = alphabet = list(string.ascii_uppercase) province_alphabet = ["A","B","C","E","G","H","J","K","L","M","N","P","R","S","T","V","X","Y"] nonLetters=["D", "F", "I", "O", "Q", "U"] second_letter= list(set(string.ascii_uppercase)-set(nonLetters)) for letter1 in province_alphabet: for number in [0,1,2,3,4,5,6,7,8,9]: for letter2 in alphabet: fsa_list.append(letter1+str(number)+letter2)
We now have our simulated FSAs!
import random random.sample(fsa_list,10)
['A0I', 'S7H', 'R5O', 'V4T', 'R9G', 'B6W', 'T6D', 'B9O', 'M0B', 'Y7H']