"Qwiklabs Assessment: Working with Regular Expressions" Code Answer's

You're definitely familiar with the best coding language Javascript that developers use to develop their projects and they get all their queries like "Qwiklabs Assessment: Working with Regular Expressions" answered properly. Developers are finding an appropriate answer about Qwiklabs Assessment: Working with Regular Expressions related to the Javascript coding language. By visiting this online portal developers get answers concerning Javascript codes question like Qwiklabs Assessment: Working with Regular Expressions. Enter your desired code related query in the search bar and get every piece of information about Javascript code related question on Qwiklabs Assessment: Working with Regular Expressions. 

Qwiklabs Assessment: Working with Regular Expressions

By Splendid SquirrelSplendid Squirrel on Oct 18, 2020
#!/usr/bin/env python3

import re
import csv

def contains_domain(address, domain):
  """Returns True if the email address contains the given,domain,in the domain position, false if not."""
  domain = r'[\w\.-]+@'+domain+'$'
  if re.match(domain,address):
    return True
  return False

def replace_domain(address, old_domain, new_domain):
  """Replaces the old domain with the new domain in the received address."""
  old_domain_pattern = r'' + old_domain + '$'
  address = re.sub(old_domain_pattern, new_domain, address)
  return address

def main():
  """Processes the list of emails, replacing any instances of the old domain with the new domain."""
  old_domain, new_domain = 'abc.edu', 'xyz.edu'
  csv_file_location = '<csv_file_location>'
  report_file = '<path_to_home_directory>' + '/updated_user_emails.csv'
  user_email_list = []
  old_domain_email_list = []
  new_domain_email_list = []

  with open(csv_file_location, 'r') as f:
    user_data_list = list(csv.reader(f))
    user_email_list = [data[1].strip() for data in user_data_list[1:]]

    for email_address in user_email_list:
      if contains_domain(email_address, old_domain):
        old_domain_email_list.append(email_address)
        replaced_email = replace_domain(email_address,old_domain,new_domain)
        new_domain_email_list.append(replaced_email)

    email_key = ' ' + 'Email Address'
    email_index = user_data_list[0].index(email_key)

    for user in user_data_list[1:]:
      for old_domain, new_domain in zip(old_domain_email_list, new_domain_email_list):
        if user[email_index] == ' ' + old_domain:
          user[email_index] = ' ' + new_domain
  f.close()

  with open(report_file, 'w+') as output_file:
    writer = csv.writer(output_file)
    writer.writerows(user_data_list)
    output_file.close()

main()

Source: roboticswithpython.com

Add Comment

0

All those coders who are working on the Javascript based application and are stuck on Qwiklabs Assessment: Working with Regular Expressions can get a collection of related answers to their query. Programmers need to enter their query on Qwiklabs Assessment: Working with Regular Expressions related to Javascript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about Qwiklabs Assessment: Working with Regular Expressions for the programmers working on Javascript code while coding their module. Coders are also allowed to rectify already present answers of Qwiklabs Assessment: Working with Regular Expressions while working on the Javascript language code. Developers can add up suggestions if they deem fit any other answer relating to "Qwiklabs Assessment: Working with Regular Expressions". Visit this developer's friendly online web community, CodeProZone, and get your queries like Qwiklabs Assessment: Working with Regular Expressions resolved professionally and stay updated to the latest Javascript updates. 

Javascript answers related to "Qwiklabs Assessment: Working with Regular Expressions"

View All Javascript queries

Javascript queries related to "Qwiklabs Assessment: Working with Regular Expressions"

Browse Other Code Languages

CodeProZone