Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 programming

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 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:
Where each segment of information is delimited by a single space. The columns represent
the following information:
The city to travel from
The city to travel to
The actual distance between the cities (-1 indicates direct driving between the
cities is not possible)
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,
].
this is the starter code
import sys
print('Number of arguments:', len(sys.argv), 'arguments.')
print('Argument List:', str(sys.argv[1:]))
filename = sys.argv[0]
country_map ={}
with open(filename) as f:
for line in f:
content_list = line.strip().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[city1]= city1_list
else:
country_map[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[city2]= city2_list
else:
country_map[city2]=[(city1, int(drivingD), int(straightD))]
for city, connections in country_map.items():
for connection in connections:
print(city, connection[0], connection[1], connection[2])
need help
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions