diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json new file mode 100644 index 0000000..f8b4888 --- /dev/null +++ b/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": null +} \ No newline at end of file diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100644 index 0000000..6b61141 --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,6 @@ +{ + "ExpandedNodes": [ + "" + ], + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/.vs/learning-python/v16/.suo b/.vs/learning-python/v16/.suo new file mode 100644 index 0000000..4cdb5f6 Binary files /dev/null and b/.vs/learning-python/v16/.suo differ diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite new file mode 100644 index 0000000..af74065 Binary files /dev/null and b/.vs/slnx.sqlite differ diff --git a/Ch2 - Basics/challenge.py b/Ch2 - Basics/challenge.py new file mode 100644 index 0000000..8e61078 --- /dev/null +++ b/Ch2 - Basics/challenge.py @@ -0,0 +1,25 @@ +# Challenge program + +def palindrome(teststring): + newstring = teststring[::-1] + return teststring == newstring + +running = True +while(running): + teststring = input("Input a string: ") + + # Should we exit? + if teststring == 'exit': + break + + # Force to lowercase + teststring = teststring.lower() + + # Remove punctuation + newstr = "" + for x in teststring: + if x.isalnum(): + newstr += x + + # Return the result + print("Palindrome?", palindrome(newstr)) \ No newline at end of file diff --git a/Ch2 - Basics/classes_start.py b/Ch2 - Basics/classes_start.py index de3226d..f822801 100644 --- a/Ch2 - Basics/classes_start.py +++ b/Ch2 - Basics/classes_start.py @@ -2,4 +2,51 @@ # Example file for working with classes # LinkedIn Learning Python course by Joe Marini # +class Vehicle(): + def __init__(self, bodystyle): + self.bodystyle = bodystyle + + def drive(self, speed): + self.mode = "driving" + self.speed = speed + +class Car(Vehicle): + def __init__(self, enginetype): + super().__init__("Car") + self.wheels = 4 + self.doors = 4 + self.enginetype = enginetype + + def drive(self, speed): + super().drive(speed) + print("Driving my", self.enginetype,"car at speed",self.speed) + + +class Motorcycle(Vehicle): + def __init__(self, enginetype, hassidecar): + super().__init__("Motorcycle") + self.hassidecare = hassidecar + if(hassidecar): + self.wheels = 3 + else: + self.wheels = 2 + self.doors = 0 + self.enginetype = enginetype + + def drive(self, speed): + super().drive(speed) + print("Driving my", self.enginetype,"motorcycle at speed",self.speed) + + +car1 = Car("gas") +car2 = Car("electric") +mc1 = Motorcycle("gas", True) +print(mc1.wheels) +print(car1.wheels) +print(mc1.enginetype) +car1.drive(30) +car2.drive(40) +mc1.drive(50) + + diff --git a/Ch2 - Basics/conditionals_start.py b/Ch2 - Basics/conditionals_start.py index f6b58d6..f255411 100644 --- a/Ch2 - Basics/conditionals_start.py +++ b/Ch2 - Basics/conditionals_start.py @@ -5,15 +5,39 @@ +from types import ClassMethodDescriptorType + + def main(): - x, y = 10, 100 + x, y = 100, 100 # conditional flow uses if, elif, else + if x < y: + result = "x is less than y" + elif x == y: + result = "x is the same as y" + else: + result = "x is greater than y" + + print(result) # conditional statements let you use "a if C else b" + result = "x is less than y" if x < y else "x is greater or equal to y" + print(result) # match-case makes it easy to compare multiple values - value = "one" + value = "George" + match value: + case "one": + result = 1 + case "two": + result = 2 + case "three" | "four": + result = (3, 4) + case _: # default + result = -1 + + print(result) if __name__ == "__main__": main() diff --git a/Ch2 - Basics/functions_start.py b/Ch2 - Basics/functions_start.py index 56cb247..7091d09 100644 --- a/Ch2 - Basics/functions_start.py +++ b/Ch2 - Basics/functions_start.py @@ -5,17 +5,46 @@ # TODO: define a basic function - +def func1(): + print("I am in func1") # TODO: function that takes arguments - +def func2(arg1, arg2): + print(arg1," ",arg2) # TODO: function that returns a value +def cube(x): + return x * x * x # TODO: function with default value for an argument +def power(num, x=1): + result = 1 + for i in range(x): + result = result * num + return result # TODO: function with variable number of arguments +def multi_add(*args): + result = 0 + for x in args: + result = result + x + return result + + + + +func1() +print(func1()) +print(func1) + +func2(10, 20) +print(func2(10, 20)) +print(cube(3)) +print(power(2)) +print(power(2,3)) +print(power(x = 3, num = 2)) +print(multi_add(4,5,10,4)) diff --git a/Ch2 - Basics/helloworld_start.py b/Ch2 - Basics/helloworld_start.py index 7d6b753..1dcef0c 100644 --- a/Ch2 - Basics/helloworld_start.py +++ b/Ch2 - Basics/helloworld_start.py @@ -1,6 +1,10 @@ # # Example file for HelloWorld # LinkedIn Learning Python course by Joe Marini -# - +def Hello(): + print("Hello World!") + name = input("What is your name?") + print("Nice to meet you", name) +if __name__ == "__main__": + Hello() diff --git a/Ch2 - Basics/loops_start.py b/Ch2 - Basics/loops_start.py index f7d2e75..20a69db 100644 --- a/Ch2 - Basics/loops_start.py +++ b/Ch2 - Basics/loops_start.py @@ -8,20 +8,30 @@ def main(): x = 0 # TODO: define a while loop - + while(x < 5): + print(x) + x += 1 # TODO: define a for loop - + for x in range(5, 10): + print(x) # TODO: use a for loop over a collection days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] - + for d in days: + print(d) + # TODO: use the break and continue statements - + for x in range(5, 10): + if (x % 2 == 0): + continue + print(x) + # TODO: using the enumerate() function to get index - + for i,d in enumerate(days): + print(i, d) if __name__ == "__main__": main() diff --git a/Ch2 - Basics/modules_start.py b/Ch2 - Basics/modules_start.py index 8c8bf6c..532912a 100644 --- a/Ch2 - Basics/modules_start.py +++ b/Ch2 - Basics/modules_start.py @@ -3,12 +3,16 @@ # TODO: import the math module, which contains features for working with mathematics - +import math # TODO: the math module contains lots of pre-built functions - +print("The square root of 16 is:", math.sqrt(16)) # TODO: in addition to functions, some modules contain useful constants - +print("Pi is",math.pi) # TODO: try some of the math functions for yourself here: +print(math.exp(0)) +print(math.exp(1)) +print(math.factorial(300)) + diff --git a/Ch3 - Files/archive.zip b/Ch3 - Files/archive.zip new file mode 100644 index 0000000..30d7a53 Binary files /dev/null and b/Ch3 - Files/archive.zip differ diff --git a/Ch3 - Files/challenge.py b/Ch3 - Files/challenge.py new file mode 100644 index 0000000..a08b9ba --- /dev/null +++ b/Ch3 - Files/challenge.py @@ -0,0 +1,29 @@ +import os +from os import path + +# Get a list of all the files in the current directory +dirlist = os.listdir() + +# Start a running total of byte count for all files +bytecount = 0 +# For each file, add its byte count to the total. Skip directories +for file in dirlist: + if path.isfile(file): + bytecount += path.getsize(path.realpath(file)) +print("Bytecount = " + str(bytecount)) + +# Then create a new subdirectory called "results" +if path.exists("results") == False: + os.mkdir("results") + +# In this directory, create a file called "results.txt" +outfile = open("./results/results.txt", "w+") + +# In results.txt, print the "Total bytecount" and a list of the filenames +outfile.write("Total Bytecount: " + str(bytecount) + "\n") +outfile.write("File List\n---------\n") +for file in dirlist: + if path.isfile(file): + outfile.write(file + "\n") + +outfile.close() diff --git a/Ch3 - Files/file.txt.bak b/Ch3 - Files/file.txt.bak new file mode 100644 index 0000000..8f4befd --- /dev/null +++ b/Ch3 - Files/file.txt.bak @@ -0,0 +1,80 @@ +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text diff --git a/Ch3 - Files/files_start.py b/Ch3 - Files/files_start.py index fb026bb..222aca4 100644 --- a/Ch3 - Files/files_start.py +++ b/Ch3 - Files/files_start.py @@ -9,15 +9,24 @@ def main(): # Open the file for appending text to the end - + file = open("file.txt","a+") # write some lines of data to the file - + for i in range(10): + file.write("This is a line of text\n") # close the file when done - + file.close() # Open the file back up and read the contents + myfile = open("file.txt", "r") + if myfile.mode == "r": + contents = myfile.read() + # contents = myfile.readlines() + # for x in contents: + # print(x) + + print(contents) if __name__ == "__main__": diff --git a/Ch3 - Files/newfile.txt b/Ch3 - Files/newfile.txt new file mode 100644 index 0000000..8f4befd --- /dev/null +++ b/Ch3 - Files/newfile.txt @@ -0,0 +1,80 @@ +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text diff --git a/Ch3 - Files/ospathutils_start.py b/Ch3 - Files/ospathutils_start.py index 3384cbd..3f5bbb1 100644 --- a/Ch3 - Files/ospathutils_start.py +++ b/Ch3 - Files/ospathutils_start.py @@ -12,19 +12,28 @@ def main(): # Print the name of the OS - + print(os.name) # Check for item existence and type - - + print("Item exists: ", path.exists("file.txt")) + print("Item is a file: ", path.isfile("file.txt")) + print("Item is a directory: ", path.isdir("file.txt")) # Work with file paths + print("Item's path: ", path.realpath("file.txt")) + print("Item's relative path: ", path.relpath("file.txt")) + print("File's path and name: ", path.split(path.realpath("file.txt"))) # Get the modification time + # Convert (ctime) to a readable string, print it out + t = time.ctime(path.getmtime("file.txt")) + print(t) + print(datetime.datetime.fromtimestamp(path.getmtime("file.txt"))) - # Calculate how long ago the item was modified - + td = datetime.datetime.now() - datetime.datetime.fromtimestamp(path.getmtime("file.txt")) + print("It has been", td, "since the file was modified") + print("Or,", td.total_seconds(), "seconds") if __name__ == "__main__": main() diff --git a/Ch3 - Files/results/results.txt b/Ch3 - Files/results/results.txt new file mode 100644 index 0000000..0d18299 --- /dev/null +++ b/Ch3 - Files/results/results.txt @@ -0,0 +1,16 @@ +Total Bytecount: 24752 +File List +--------- +archive.zip +challenge.py +challenge_solution.py +file.txt.bak +files_finished.py +files_start.py +newfile.txt +ospathutils_finished.py +ospathutils_start.py +shell_finished.py +shell_start.py +testzip.zip +textfile.txt diff --git a/Ch3 - Files/shell_start.py b/Ch3 - Files/shell_start.py index 5cb9ec5..522906f 100644 --- a/Ch3 - Files/shell_start.py +++ b/Ch3 - Files/shell_start.py @@ -5,20 +5,33 @@ import os from os import path +import shutil +from shutil import make_archive +from zipfile import ZipFile def main(): # make a duplicate of an existing file - if path.exists("textfile.txt"): + if path.exists("file.txt.bak"): # get the path to the file in the current directory - + src = path.realpath("file.txt") + # let's make a backup copy by appending "bak" to the name - + # dst = src + ".bak" + # shutil.copy(src, dst) + # rename the original file + # os.rename("file.txt", "textfile.txt") + # shutil.copy("textfile.txt", "newfile.txt") # now put things into a ZIP archive + # root_dir, tail = path.split(src) + # shutil.make_archive("archive", "zip", root_dir) # more fine-grained control over ZIP files - + with ZipFile("testzip.zip","w") as newzip: + newzip.write("textfile.txt") + newzip.write("newfile.txt") + newzip.write("file.txt.bak") if __name__ == "__main__": main() diff --git a/Ch3 - Files/testzip.zip b/Ch3 - Files/testzip.zip new file mode 100644 index 0000000..5a6c841 Binary files /dev/null and b/Ch3 - Files/testzip.zip differ diff --git a/Ch3 - Files/textfile.txt b/Ch3 - Files/textfile.txt new file mode 100644 index 0000000..8f4befd --- /dev/null +++ b/Ch3 - Files/textfile.txt @@ -0,0 +1,80 @@ +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text diff --git a/Ch4 - Dates and Times/calendars_start.py b/Ch4 - Dates and Times/calendars_start.py index 2963f70..eba080d 100644 --- a/Ch4 - Dates and Times/calendars_start.py +++ b/Ch4 - Dates and Times/calendars_start.py @@ -5,12 +5,17 @@ # TODO: import the calendar module - +import calendar # TODO: create a plain text calendar - +c = calendar.TextCalendar(calendar.MONDAY) +str = c.formatmonth(2022, 1, 0, 0) +print(str) # TODO: create an HTML formatted calendar +hc = calendar.HTMLCalendar(calendar.SUNDAY) +str = hc.formatmonth(2022, 1) +print(str) # TODO: loop over the days of a month diff --git a/Ch4 - Dates and Times/challenge_start.py b/Ch4 - Dates and Times/challenge_start.py index 9da42cb..487d900 100644 --- a/Ch4 - Dates and Times/challenge_start.py +++ b/Ch4 - Dates and Times/challenge_start.py @@ -3,3 +3,42 @@ # import calendar + +def countdays(year, month, day): + daycount = 0 + c = calendar.monthcalendar(year, month) + for row in c: + if row[day] != 0: + daycount += 1 + return daycount + +running = True +try: + while(running == True): + print("Enter the day of the week:") + print("0 - Monday") + print("1 - Tuesday") + print("2 - Wednesday") + print("3 - Thursday") + print("4 - Friday") + print("5 - Saturday") + print("6 - Sunday") + + entry = input("? ") + + if entry == "exit": + running = False + break + day = int(entry) + # Get the month and year + month = input("Enter the month 1 - 12: ") + month = int(month) + year = input("Enter the year: ") + year = int(year) + + result = countdays(year, month, day) + print("There are " + str(result) + " days in the month") +except Exception as e: + print("Invalid input") + print(e) + diff --git a/Ch4 - Dates and Times/dates_start.py b/Ch4 - Dates and Times/dates_start.py index 9091c40..0d5a9a0 100644 --- a/Ch4 - Dates and Times/dates_start.py +++ b/Ch4 - Dates and Times/dates_start.py @@ -3,24 +3,35 @@ # LinkedIn Learning Python course by Joe Marini # - +from datetime import date +from datetime import time +from datetime import datetime def main(): ## DATE OBJECTS # TODO: Get today's date from the simple today() method from the date class + today = date.today() + print("Today's date is ", today) + - # TODO: print out the date's individual components + # TODO: print out the date's individual components + print("Date compoents: ", today.day, today.month, today.year) # TODO: retrieve today's weekday (0=Monday, 6=Sunday) - + print("Today's weekday number: ", today.weekday()) + days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] + print("which is a ", days[today.weekday()]) ## DATETIME OBJECTS # TODO: Get today's date from the datetime class - + today = datetime.now() + print(today) # TODO: Get the current time + t = datetime.time(datetime.now()) + print("Current time is ", t) diff --git a/Ch4 - Dates and Times/formatting_start.py b/Ch4 - Dates and Times/formatting_start.py index 6c40839..e8da541 100644 --- a/Ch4 - Dates and Times/formatting_start.py +++ b/Ch4 - Dates and Times/formatting_start.py @@ -12,12 +12,13 @@ def main(): #### Date Formatting #### - - # %y/%Y - Year, %a/%A - weekday, %b/%B - month, %d - day of month + now = datetime.now() + # %y/%Y - Year, %a/%A - weekday, %b/%B - month, %d - day of month + print(now.strftime("%a %B %d %Y")) # %c - locale's date and time, %x - locale's date, %X - locale's time - + print(now.strftime("%c which is %x and %X")) #### Time Formatting #### diff --git a/Ch4 - Dates and Times/timedeltas_start.py b/Ch4 - Dates and Times/timedeltas_start.py index a6b62bc..54d352d 100644 --- a/Ch4 - Dates and Times/timedeltas_start.py +++ b/Ch4 - Dates and Times/timedeltas_start.py @@ -7,27 +7,36 @@ from datetime import date from datetime import time from datetime import datetime +from datetime import timedelta # TODO: construct a basic timedelta and print it - +print(timedelta(days = 365, hours = 5, minutes = 1)) # TODO: print today's date +now = datetime.now() +print(now) # TODO: print today's date one year from now - +print("365 days from now it will be ", str(now + timedelta(days = 365))) # TODO: create a timedelta that uses more than one argument - +print("In two weeks and three days it will be ", str(now + timedelta(weeks = 2, days = 3))) # TODO: calculate the date 1 week ago, formatted as a string - +print("One week ago it was "+ str(now - timedelta(weeks =1))) ### How many days until April Fools' Day? +today = date.today() +afd = date(today.year, 4, 1) # TODO: use date comparison to see if April Fool's has already gone for this year +if (afd < today): + afd.replace(year = today.year + 1) +time_to_afd = afd - today +print("There are ", time_to_afd.days, "to the next AFD") # if it has, use the replace() function to get the date for next year diff --git a/Ch5 - Internet Data/htmlparsing_start.py b/Ch5 - Internet Data/htmlparsing_start.py index a759ac3..73594a7 100644 --- a/Ch5 - Internet Data/htmlparsing_start.py +++ b/Ch5 - Internet Data/htmlparsing_start.py @@ -4,16 +4,35 @@ # from html.parser import HTMLParser +# global +paragraphs = 0 class MyHTMLParser(HTMLParser): def handle_comment(self, data): - pass + print("Encountered a comment: ", data) + pos = self.getpos() + print("at line ", pos[0], "position ", pos[1]) def handle_starttag(self, tag, attrs): - pass + print("Encountered a starttag: ", tag) + pos = self.getpos() + print("at line ", pos[0], "position ", pos[1]) + #use the global + global paragraphs + if tag == "p": + paragraphs += 1 + if len(attrs) > 0: + print("Tag attributes:") + for a in attrs: + print("\t", a[0], "=", a[1]) + def handle_data(self, data): - pass + if data.isspace(): + return + print("Encountered data: ", data) + pos = self.getpos() + print("at line ", pos[0], "position ", pos[1]) def main(): # instantiate the parser and feed it some HTML @@ -23,6 +42,7 @@ def main(): if f.mode == "r": contents = f.read() # read the entire file parser.feed(contents) + print("Paragraph tags: ", paragraphs) if __name__ == "__main__": main() diff --git a/Ch5 - Internet Data/inetdata_start.py b/Ch5 - Internet Data/inetdata_start.py index 86dc094..f574379 100644 --- a/Ch5 - Internet Data/inetdata_start.py +++ b/Ch5 - Internet Data/inetdata_start.py @@ -2,9 +2,14 @@ # Example file for retrieving data from the internet # LinkedIn Learning Python course by Joe Marini # +import urllib.request def main(): - pass # this is a placeholder, do-nothing statement + weburl = urllib.request.urlopen("http://www.google.com") + print("Result code: ", weburl.getcode()) + data = weburl.read() + print("Page data:\n", data) + if __name__ == "__main__": main() diff --git a/Ch5 - Internet Data/jsondata_start.py b/Ch5 - Internet Data/jsondata_start.py index e0da623..c995fb5 100644 --- a/Ch5 - Internet Data/jsondata_start.py +++ b/Ch5 - Internet Data/jsondata_start.py @@ -4,25 +4,39 @@ # import urllib.request +import json def printResults(data): # Use the json module to load the string data into a dictionary theJSON = json.loads(data) # now we can access the contents of the JSON like any other Python object - + if "title" in theJSON["metadata"]: + print(theJSON["metadata"]["title"]) # output the number of events, plus the magnitude and each event name - + count = theJSON["metadata"]["count"] + print(count, "events recorded") # for each event, print the place where it occurred + for i in theJSON["features"]: + print(i["properties"]["place"]) + print("..............\n") # print the events that only have a magnitude greater than 4 - - + for i in theJSON["features"]: + if i["properties"]["mag"] >= 4.0: + print(i["properties"]["place"]) + print("..............\n") # print only the events where at least 1 person reported feeling something - + print("Events that were felt and reported:") + for i in theJSON["features"]: + feltReports = i["properties"]["felt"] + if feltReports != None: + if feltReports > 0: + print(i["properties"]["place"], feltReports, "times") + print("..............\n") def main(): # define a variable to hold the source URL @@ -33,6 +47,11 @@ def main(): # Open the URL and read the data webUrl = urllib.request.urlopen(urlData) print ("result code: " + str(webUrl.getcode())) + if (webUrl.getcode() == 200): + data = webUrl.read() + printResults(data) + else: + print("Error from the server, cannot print results: ", webUrl.getcode()) if __name__ == "__main__": diff --git a/Ch5 - Internet Data/xmlparsing_start.py b/Ch5 - Internet Data/xmlparsing_start.py index 3129b0c..985a2ca 100644 --- a/Ch5 - Internet Data/xmlparsing_start.py +++ b/Ch5 - Internet Data/xmlparsing_start.py @@ -2,21 +2,31 @@ # Example file for parsing and processing XML # LinkedIn Learning Python course by Joe Marini # - +import xml.dom.minidom def main(): # use the parse() function to load and parse an XML file - + doc = xml.dom.minidom.parse("samplexml.xml") # print out the document node and the name of the first child tag - + print(doc.nodeName) + print(doc.firstChild.tagName) # get a list of XML tags from the document and print each one + skills = doc.getElementsByTagName("skill") + print("There are ", skills.length, "skills listed") + for skill in skills: + print(skill.getAttribute("name")) # create a new XML tag and add it into the document - - + newskill = doc.createElement("skill") + newskill.setAttribute("name","jQuery") + doc.firstChild.appendChild(newskill) + skills = doc.getElementsByTagName("skill") + print("There are now ", skills.length, "skills listed") + for skill in skills: + print(skill.getAttribute("name")) if __name__ == "__main__": main() diff --git a/file.txt b/file.txt new file mode 100644 index 0000000..97a5716 --- /dev/null +++ b/file.txt @@ -0,0 +1,140 @@ +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text +This is a line of text