This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
def scrape_page(url): | |
response = requests.get(url) | |
soup = BeautifulSoup(response.text,'lxml') | |
print(soup) | |
quotes = soup.find_all("span", class_="text") | |
authors = soup.find_all("small", class_="author") | |
tags = soup.find_all("div", class_="tags") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fileIO(inputFile='inputFile.txt'): | |
f = open(inputFile,'r') | |
output1 = "PassFile.txt" | |
output2 = "FailFile.txt" | |
passFile = open(output1,'w') | |
failFile = open(output2, 'w') | |
for line in f: | |
line_split = line.split() | |
if line_split[2] == "P": |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
list_2d = [['Volkswagen', 'Mercedes', 'BMW'], ['Honda', 'Toyota', 'Mazda']] | |
def convertList(list_2d): | |
return list(numpy.concatenate(list_2d).flat) | |
print(convertList(list_2d)) # Output ['Volkswagen', 'Mercedes', 'BMW', 'Honda', 'Toyota', 'Mazda'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def convert_phone_number(phone): | |
result = re.sub(r"\b(\d{3})-(\d{3})-(\d{4})\b",r"(\1) \2-\3", phone) | |
return result | |
print(convert_phone_number("My number is 212-345-9999.")) # My number is (212) 345-9999. | |
print(convert_phone_number("Please call 888-555-1234")) # Please call (888) 555-1234 | |
print(convert_phone_number("123-123-12345")) # 123-123-12345 | |
print(convert_phone_number("Phone number of Buckingham Palace is +44 303 123 7300")) # Phone number of Buckingham Palace is +44 303 123 7300 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def multi_vowel_words(text): | |
pattern = r"(\w+[a,e,i,o,u]{3,}\w+)" | |
result = re.findall(pattern, text) | |
return result | |
print(multi_vowel_words("Life is beautiful")) | |
# ['beautiful'] | |
print(multi_vowel_words("Obviously, the queen is courageous and gracious.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def extract_pid(log_line): | |
regex = r"\[(\d+)\]\: (\w+)" | |
result = re.search(regex, log_line) | |
if result is None: | |
return None | |
return "{} ({})".format(result[1], result[2]) | |
print(extract_pid("July 31 07:51:48 mycomputer bad_process[12345]: ERROR Performing package upgrade")) # 12345 (ERROR) | |
print(extract_pid("99 elephants in a [cage]")) # None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def long_words(text): | |
pattern = r"\w{7,}" | |
result = re.findall(pattern, text) | |
return result | |
print(long_words("I like to drink coffee in the morning.")) # ['morning'] | |
print(long_words("I also have a taste for hot chocolate in the afternoon.")) # ['chocolate', 'afternoon'] | |
print(long_words("I never drink tea late at night.")) # [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def rearrange_name(name): | |
result = re.search(r"^([\w \.-]*), ([\w \.-]*)$", name) | |
if result == None: | |
return name | |
return "{} {}".format(result[2], result[1]) | |
name=rearrange_name("Kennedy, John F.") | |
print(name) |
NewerOlder