Answered step by step
Verified Expert Solution
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 indicates direct driving between the
cities is not possible
The straightline 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 eg 'Melbourne' and the value is a list of cities whose information
can be extracted from the input text file egSydneyBrisbane
this is the starter code
import sys
printNumber of arguments: lensysargv 'arguments.
printArgument List: strsysargv:
filename sysargv
countrymap
with openfilename as f:
for line in f:
contentlist line.stripsplit
city contentlist
city contentlist
drivingD contentlist
straightD contentlist
if countrymap.getcity:
citylist countrymap.getcity
citylist.appendcity intdrivingD intstraightD
countrymapcity citylist
else:
countrymapcitycity intdrivingD intstraightD
if countrymap.getcity:
citylist countrymap.getcity
citylist.appendcity intdrivingD intstraightD
countrymapcity citylist
else:
countrymapcitycity intdrivingD intstraightD
for city, connections in countrymap.items:
for connection in connections:
printcity connection connection connection
need help
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started