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...
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
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
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT