Reference no: EM132261384
Please explain and provide example of how do I rewrite a python program as a text file?
Here is my program in Unit 5.
# List of strings representing city, state zip
# One space after the longest city name
# Adds two additional test cases
# Information in a nicely formatted table
def main():
mycities = ['Cape Girardeau, MO 63780','Columbia, MO 65201',
'Kansas City, MO 64108','Rolla,MO 65402',
'Springfield, MO65897','St Joseph, MO64504',
'St Louis,MO63111', 'Ames, IA 50010', 'Enid, OK 73773',
'West Palm Beach, FL 33412',
'International Falls, MN 56649', 'Frostbite Falls, MN 56650',
'Chattahoochee Hills, GA 30268', 'Lava Hot Springs, ID 83246'
]
# Prints table
for c in mycities:
#Slice that is the city
city = c[:c.index(",")]
#Slice that is the state abbreviation
state = c[c.index(",") + 1:-5].strip()
#state = state.strip()
#Slice that is the area code, last five numbers
zipcode = c[-5:]
#Reformmated string using the above stated variables
formatted_string = "{}{} {}".format(city.ljust(20), state, zipcode)
#formatted_string = "{:<{}}".format(20, mycities)
print(formatted_string)
main()
My next assignment is asking to do this:
1.This assignment must be done in a standalone Python environment
2.Download the data file CityStateData.txt. This is the same set of data that you used in the previous assignment, but stored in a text file instead. Put this text file in the same folder as your Python program.
Here is the CityStateData text file:
Cape Girardeau, MO 63780
Columbia, MO 65201
Kansas City, MO 64108
Rolla,MO 65402
Springfield, MO65897
St. Joseph, MO64504
St Louis,MO63111
Ames, IA 50010
Enid, OK 73773
West Palm Beach, FL 33412
International Falls, MN 56649
Frostbite Falls,MN 56650
- Rewrite your program from Unit 5 so that it reads myCities from a file instead of defining all the data as part of your program itself.
- The file aspect should be the only thing that you change - the rest of the program logic should remain the same.
- Add your two test cases from the program in Unit 5 (these are the cities I added).
Chattahoochee Hills, GA 30268', 'Lava Hot Springs, ID 83246
- Add two more cities of your own at the end of the text file.
- You can edit the text file with Notepad, or IDLE, or any text editor on your system.