update
This commit is contained in:
parent
04283eec4a
commit
f2eb6e863f
@ -672,6 +672,96 @@
|
||||
"print(convert_phone_number(\"123-123-12345\")) # 123-123-12345\n",
|
||||
"print(convert_phone_number(\"Phone number of Buckingham Palace is +44 303 123 7300\")) # Phone number of Buckingham Palace is +44 303 123 7300"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Glossary\n",
|
||||
"### Terms and definitions from Course 2, Module 3\n",
|
||||
"**Alteration:** RegEx that matches any one of the alternatives separated by the pipe symbol.\n",
|
||||
"\n",
|
||||
"**Backreference:** This is applied when using re.sub( ) to substitute the value of a capture group into the output.\n",
|
||||
"\n",
|
||||
"**Character classes:** These are written inside square brackets and let us list the characters we want to match inside of those brackets.\n",
|
||||
"\n",
|
||||
"**Character ranges:** Ranges used to match a single character against a set of possibilities.\n",
|
||||
"\n",
|
||||
"**grep:** An especially easy to use yet extremely powerful tool for applying RegExes.\n",
|
||||
"\n",
|
||||
"**Lookahead:** RegEx that matches a pattern only if it’s followed by another pattern.\n",
|
||||
"\n",
|
||||
"**Regular expression:** A search query for text that's expressed by string pattern, also known as RegEx or RegExp.\n",
|
||||
"\n",
|
||||
"**Wildcard:** A character that can match more than one character.\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Lab"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#!/usr/bin/env python3\n",
|
||||
"import re\n",
|
||||
"import csv\n",
|
||||
"\n",
|
||||
"def contains_domain(address, domain):\n",
|
||||
" \"\"\"Returns True if the email address contains the given,domain,in the domain position, false if not.\"\"\"\n",
|
||||
" domain_pattern = r'[\\w\\.-]+@'+domain+'$'\n",
|
||||
" if re.match(domain_pattern,address):\n",
|
||||
" return True\n",
|
||||
" return False\n",
|
||||
"\n",
|
||||
"def replace_domain(address, old_domain, new_domain):\n",
|
||||
" \"\"\"Replaces the old domain with the new domain in the received address.\"\"\"\n",
|
||||
" old_domain_pattern = r'' + old_domain + '$'\n",
|
||||
" address = re.sub(old_domain_pattern, new_domain, address)\n",
|
||||
" return address\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" \"\"\"Processes the list of emails, replacing any instances of the old domain with the new domain.\"\"\"\n",
|
||||
" old_domain, new_domain = 'abc.edu', 'xyz.edu'\n",
|
||||
" csv_file_location = '<csv_file_location>'\n",
|
||||
" report_file = '<data-directory>' + '/updated_user_emails.csv'\n",
|
||||
" user_email_list = []\n",
|
||||
" old_domain_email_list = []\n",
|
||||
" new_domain_email_list = []\n",
|
||||
"\n",
|
||||
" with open(csv_file_location, 'r') as f:\n",
|
||||
" user_data_list = list(csv.reader(f))\n",
|
||||
" user_email_list = [data[1].strip() for data in user_data_list[1:]]\n",
|
||||
"\n",
|
||||
" for email_address in user_email_list:\n",
|
||||
" if contains_domain(email_address, old_domain):\n",
|
||||
" old_domain_email_list.append(email_address)\n",
|
||||
" replaced_email = replace_domain(email_address,old_domain,new_domain)\n",
|
||||
" new_domain_email_list.append(replaced_email)\n",
|
||||
"\n",
|
||||
" email_key = ' ' + 'Email Address'\n",
|
||||
" email_index = user_data_list[0].index(email_key)\n",
|
||||
"\n",
|
||||
" for user in user_data_list[1:]:\n",
|
||||
" for old_domain, new_domain in zip(old_domain_email_list, new_domain_email_list):\n",
|
||||
" if user[email_index] == ' ' + old_domain:\n",
|
||||
" user[email_index] = ' ' + new_domain\n",
|
||||
" f.close()\n",
|
||||
"\n",
|
||||
" with open(report_file, 'w+') as output_file:\n",
|
||||
" writer = csv.writer(output_file)\n",
|
||||
" writer.writerows(user_data_list)\n",
|
||||
" output_file.close()\n",
|
||||
"\n",
|
||||
"main()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
Loading…
Reference in New Issue
Block a user