Question

In: Computer Science

Question6: Write a program to read the file 202010mid.txt, store the content in a list of...

Question6: Write a program to read the file 202010mid.txt, store the content in a list of tuples, then print the list. [6 marks]

Question7: Write Python code to do the following operations using request library. [12 marks]

Check if the following webpage is available or not: https://edition.cnn.com/news.html [4 marks]
Send a get request to the following webpage and show the result: http://api.open-notify.org/iss-pass.json [4 marks]
The webpage from part 2 is expecting some parameters as shown from the results. Now create a parameter with two key:value pairs as per the following: lat with value 40.71 and lon with value -74. Send a get request now with the parameter you created and display the result.[4 marks]

Solutions

Expert Solution

Q6. The question does not include 202010mid.txt. Have made assumptions about the file and written the code; if any doubts, please ask in comments:

tuplst = []
with open ("202010mid.txt", "r") as f:
    while True:
        s = f.readline()
        if s == '':
            break
        pair = s.split()
        t = (pair[0], pair[1])
        tuplst.append (t)
        
print (tuplst)
    
//////// Here is the output ////////////////////////////////////////
[('Arnold', '85'), ('Schwarz', '62'), ('Peter', '70'), ('Nick', '90'), ('Don', '99'), ('Trump', '40')]  


//////// This is for an input file that looks like this /////////////////
Arnold 85
Schwarz  62
Peter   70
Nick    90
Don 99
Trump 40

Q7. part 1:

import requests
x = requests.get ("https://edition.cnn.com/news.html")
if (x.status_code != 200):
    print ("Page not found.")

Running the above shows 'Page not found.' since the request returns a 404 error.

Q7. part 2:

import requests
url = "http://api.open-notify.org/iss-pass.json"
par = {'lat' : 40.71, 'lon' : -74}
r = requests.get (url, par)
print (r.json())

## Below are the results
{'message': 'success', 'request': {'altitude': 100, 'datetime': 1604213248, 'latitude': 40.71, 'longitude': -74.0, 'passes': 5}, 'response': [{'duration': 653, 'risetime': 1604214313}, {'duration': 596, 'risetime': 1604220175}, {'duration': 560, 'risetime': 1604226062}, {'duration': 622, 'risetime': 1604231891}, {'duration': 645, 'risetime': 1604237696}]}

Related Solutions

Write a C program that will read different data types from the following file and store...
Write a C program that will read different data types from the following file and store it in the array of structures. Given file: (This file have more than 1000 lines of similar data): time latitude longitude depth mag magType nst gap dmin 2020-10-19T23:28:33.400Z 61.342 -147.3997 12.3 1.6 ml 12 84 0.00021 2020-10-19T23:26:49.460Z 38.838501 -122.82684 1.54 0.57 md 11 81 0.006757 2020-10-19T23:17:28.720Z 35.0501667 -117.6545 0.29 1.51 ml 17 77 0.1205 2020-10-19T22:47:44.770Z 38.187 -117.7385 10.8 1.5 ml 15 100.22 0.049 2020-10-19T22:42:26.224Z...
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
n-class: Write a program to store exam scores into a file. The program will ask the...
n-class: Write a program to store exam scores into a file. The program will ask the user how many exam scores to enter. Then it will ask the user for each exam score and store them in a file called scores.dat The file output should have the following format . Exam 1: 97 Exam 2: 85 C++ Coding please
In this program you will read a file specifying a directed graph G as a list...
In this program you will read a file specifying a directed graph G as a list of edges. Each edge u →v is listed on one line as u v. The input file simply lists the edges in arbitrary order as pairs of vertices, with each edge on a separate line. The vertices are numbered in order from 1 to the total number of vertices. The program outputs the out-degree sequence for GSCC in increasing order. For example for the...
In this program you will read a file specifying a directed graph G as a list...
In this program you will read a file specifying a directed graph G as a list of edges. Each edge u →v is listed on one line as u v. The input file simply lists the edges in arbitrary order as pairs of vertices, with each edge on a separate line. The vertices are numbered in order from 1 to the total number of vertices. The program outputs the out-degree sequence for GSCC in increasing order. For example for the...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT