Question: In this programming task, you ll write a simple Python script to read a file a process the data contained in the file. Throughout the

In this programming task, youll write a simple Python script to read a file a process the
data contained in the file.
Throughout the programming aspect of the unit, numerous files will be provided, each
requiring a different method of interpretation. As an introduction to the unit, you will be
required to write a program that can parse a simple text file of the following format:
Melbourne
Melbourne
Brisbane
Sydney
Brisbane
Newcastle
878-1
780
713
1374
618
Where each segment of information is delimited by a single space. The columns represent
the following information:
1. The city to travel from
2. The city to travel to
3. The actual distance between the cities (-1 indicates direct driving between the
cities is not possible)
4. The straight-line distance to the destination
Information about each pair of cities should be read in and parsed and stored in
appropriate data structures. In particular, a Python dictionary should be created so that the
key is a city name (e.g.,Melbourne) and the value is a list of cities whose information
can be extracted from the input text file - e.g.,[(Sydney,878,713),(Brisbane,-1,
1374)].
import sys
print('Number of arguments:', len(sys.argv), 'arguments.')
print('Argument List:', str(sys.argv))
filename = sys.argv[1]
country_map ={}
with open(filename) as f:
for line in f:
content = line.strip()
content_list = content.split("")
city1= content_list[0]
city2= content_list[1]
drivingD = content_list[2]
straightD = content_list[3]
if country_map.get(city1):
city1_list = country_map.get(city1)
city1_list.append((city2, int(drivingD), int(straightD)))
country_map.update({city1: city1_list})
else:
country_map.update({city1: [(city2, int(drivingD), int(straightD))]})
if country_map.get(city2):
city2_list = country_map.get(city2)
city2_list.append((city1, int(drivingD), int(straightD)))
country_map.update({city2: city2_list})
else:
country_map.update({city2: [(city1, int(drivingD), int(straightD))]})
print(country_map)
output below
PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL
Number of arguments: 2 arguments.
Argument List: ['.\ai_input.py', 'cities.txt'] 'Newcastle': [('Brisbane',780,618)]}
 In this programming task, youll write a simple Python script to

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!