Strings can be further arranged, so they can encode more complex information. For example, CSV (Comma-Separated Values) can be used to share spreadsheet data between applications.
JSON (Javascript Object Notation) is used to transmit ordered lists (arrays) and name- value pairs (objects). It can be sent in a file or through a web request.
The syntax for a JSON array is similar to the syntax for a Python list.
["Monday", 6, "pumpkin", 3.1415]
The quotation marks around strings are mandatory.
The syntax for a JSON objecgts is similar to the syntax for Python dictionaries. ```` { "latitude_degree": 43.6617, "latitude_direction": "N", "longitude_degree": 79.3950, "longitude_direction": "W" }
Arrays and objects can appear as values within arrays and objects. Here is an example of a complex JSON string, formatted for readability by humans.
[ { "customer": { "last": "Ball", "first": "Maldonado" }, "rating": 2, "specialOrder": true, "tags": [ "ullamco", "aute", "mollit", "ex" ], "greeting": "Happy Birthday", "filling": "chocolate", "batter": "vanilla", "frosting": "chocolate" } ] ```
As you can see, it’s a customer dictionary with a nested dictionary for the address and a list of dictionaries for telephone numbers. The formatting is similar to Python.
An useful tool when working with JSON is a viewer. JSON that is being transmitted usually has all of the whitespace removed. It makes the total transmission smaller, but difficult for humans to read. It’s common to use a JSON viewer, in your IDE or on a web page. You can find one by Googling.
Another common tool is a JSON validator. It checks whether your syntax is correct and helps to localize errors. Sometimes you can use the same tool to view and validate JSON.
In [57]:
import json
list1 = ["Monday", 6, "pumpkin", 3.1415]
dict1 = {
"latitude_degree": 43.6617,
"latitude_direction": "N",
"longitude_degree": 79.3950,
"longitude_direction": "W"
}
json_encoded = json.dumps(dict1)
print json_encoded
In [59]:
import json
with open("cake.json", "r") as file_reader:
file_contents = file_reader.read()
json_contents = json.loads(file_contents)
# print json_contents
print json.dumps(json_contents, indent=1)
json_contents
is typically list, a dictionary, or a combination. You already know how to use these. You can use a for to iterate over all the elements in a list. You can use dictionary syntax to access key value pairs.
In [71]:
for order in json_contents:
print("Customer: " + order['customer']['last'])
for word in order['tags']:
print("\t" + word)
Regular expressions, called regexes for short, are descriptions for a pattern of text. These are useful for validating inputs and formatting.
For example, a \d
in a regex stands for a digit character—that is, any single numeral 0 to 9. The regex \d\d\d-\d\d\d-\d\d\d\d
is used by Python to match a telephone number as a string of three numbers, a hyphen, three more numbers, another hyphen, and four numbers. Any other string would not match the \d\d\d-\d\d\d-\d\d\d\d
regex.
But regular expressions can be much more sophisticated. For example, adding a 3 in curly brackets ({3}
) after a pattern is like saying, “Match this pattern three times.” So the slightly shorter regex \d{3}-\d{3}-\d{4}
also matches the correct phone number format.
In [72]:
import re
phone_regex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
In [81]:
phone_match = phone_regex.search('My number is 647-970-9425.')
if phone_match is None:
print ("No match found")
else:
print('Phone number found: ' + phone_match.group())
In [88]:
phone_regex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
phone_match = phone_regex.search('My number is 415-555-4242.')
print phone_match.group()
print phone_match.group(0)
print phone_match.group(1)
print phone_match.group(2)
In [87]:
print phone_match.groups()
In [90]:
area_code, main_number = phone_match.groups()
print area_code
print main_number
In [91]:
cheese_regex = re.compile(r'^Cheese')
cheese_match = cheese_regex.search('Cheese Shop sketch')
print cheese_match.group()
cheese_match = cheese_regex.search('Not much of a Cheese Shop really, is it?')
cheese_match == None
Out[91]:
In [97]:
monty_regex = re.compile(r"Terry|Michael|Graham|John|Eric")
monty_match = monty_regex.search("Eric Palin, John Cleese, and Eric Idle")
print monty_match.group(), "at" , monty_match.start()
monty_match = monty_regex.findall("Eric Palin, John Cleese, and Eric Idle")
print monty_match
In [100]:
parrot_regex = re.compile(r'Parrot(man|mobile|copter|bat)')
parrot_match = parrot_regex.search('Parrotcopter lost a blade')
parrot_match.group()
Out[100]:
In [101]:
parrot_regex = re.compile(r'Parrot(wo)?man')
parrot_match = parrot_regex.search('The Adventures of Parrotman')
print parrot_match.group()
parrot_match = parrot_regex.search('The Adventures of Parrotwoman')
print parrot_match.group()
In [102]:
parrot_regex = re.compile(r'Parrot(wo)*man')
parrot_match = parrot_regex.search('The Adventures of Parrotman')
print parrot_matches.group()
parrot_match = parrot_regex.search('The Adventures of Parrotwowowoman')
print parrot_match.group()
In [103]:
parrot_regex = re.compile(r'Parrot(wo)+man')
parrot_match = parrot_regex.search('The Adventures of Parrotwoman')
print parrot_match.group()
parrot_match = parrot_regex.search('The Adventures of Parrotman')
parrot_match == None
Out[103]:
In [104]:
# Match everything with .*
name_regex = re.compile(r'First Name: (.*) Last Name: (.*)')
name_match = name_regex.search("First Name: Tarquin Last Name: Fin-tim-lim-bim-whin-bim-lim-bus-stop-F'tang-F'tang-Ole-Biscuitbarrel")
name_match.groups()
Out[104]:
In [107]:
ha_regex = re.compile(r'(Ha){3}')
ha_match = ha_regex.search('HaHaHa')
ha_match.group()
Out[107]:
(Ha){3}
means exactly 3 repetitions
(Ha){,5}
means up to 5 repetitions
(Ha){3,5}
means 3 to 5 repetitions
In [108]:
greedy_regex = re.compile(r'(Ha){3,5}')
greedy_match = greedy_regex.search('HaHaHaHaHa')
print greedy_match.group()
nongreedy_regex = re.compile(r'(Ha){3,5}?')
nongreedy_match = nongreedy_regex.search('HaHaHaHaHa')
print nongreedy_match.group()
In [109]:
gbs_regex = re.compile(r'\d+\s\w+')
gbs_regex.findall("12 apostles, 11 went straight to heaven, \\
10 commandments, 9 bright eyed shiners, \\
8 Gabriel angels")
Out[109]:
In [110]:
vowel_regex = re.compile(r'[aeiouAEIOU]')
vowel_regex.findall('This is a dead parrot. DEAD.')
Out[110]:
In [111]:
vowel_regex = re.compile(r'[aeiou]', re.I)
vowel_regex.findall('This is a dead parrot. DEAD.')
Out[111]:
In [ ]:
# Exercise
# Joke: What do you call a pig with three eyes? Piiig!
Two roles: person asking the question (Q) and the person answering the question (A)
If you ask a question 3 times and can't get an answer, bring it to me.
In [ ]: