Question

The file Cities.txt contains information about the 25 largest cities in the United States. Each line...

The file Cities.txt contains information about the 25 largest cities in the United States. Each line of the file has four fields—name, state, population in 2000 (in 100,000s), and population in 2010 (in 100,000s). Write a python program that creates a new file with each line containing the name of a city and its percentage population growth from 2000 to 2010. The cities should be in decreasing order by their percent population growth.

The first four lines of the file Cities.txt are as follows:

New York,NY,80.1,82.7

Los Angeles,CA,36.9,38.84

Chicago,IL,29.0,28.7

Houston,TX,19.5,22.4

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER :

Code :-

fname = "/home/sasi/Desktop/pro/Python/cities.txt" #absolute path of file
fout = "/home/sasi/Desktop/pro/Python/cities_out.txt"   
listt = [] #empty list
with open(fname) as f: #opening file
lines = [line.rstrip('\n').split(',') for line in f] #splitting the file by ,
for i in range (0, len(lines)): #run till the end of file line by line
increase = float(lines[i][3]) - float(lines[i][2]) #taking the difference between 4th column and 3rd column
increase = increase / float(lines[i][2]) * 100 #calculating the percentage
listt.append("%.2f,%s,%s" % (float(increase), lines[i][0], lines[i][1])) #appending into list

listt.sort(reverse=True) #list sort in descending order
for line in listt:
y = line.strip().split(",")
with open(fout,"a") as text_file:
text_file.write("%s, %s, %.2f\n" % (y[1],y[2], float(y[0])))
text_file.close()

CODE SCREENSHOT :-

OUTPUT SCREENSHOT :-

PLEASE LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.

THANK YOU:)

Add a comment
Know the answer?
Add Answer to:
The file Cities.txt contains information about the 25 largest cities in the United States. Each line...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Cost of Living in Selected U.S. Cities Cost-of-living index City number New York (Manhattan) 216.7...

    The Cost of Living in Selected U.S. Cities Cost-of-living index City number New York (Manhattan) 216.7 San Francisco, CA 164.0 Washington, DC 140.1 Los Angeles, CA 136.4 Boston, MA 132.5 Philadelphia, PA 126.5 Seattle, WA 121.4 Chicago, IL 116.9 Richmond, VA 104.5 Phoenix, AZ 100.7 Detroit, MI 99.4 Atlanta, GA 95.6 Charlotte, NC 93.2 Dallas, TX 91.9 Source US Census Annual Average 2010 The table shown here gives a cost-of-living index for 14 different cities. Currently, you are living and...

  • The Cost of Living in Selected U.S. Cities Cost-of-living index City number New York (Manhattan) 216.7...

    The Cost of Living in Selected U.S. Cities Cost-of-living index City number New York (Manhattan) 216.7 San Francisco, CA 164.0 Washington, DC 140.1 Los Angeles, CA 136.4 Boston, MA 132.5 Philadelphia, PA 126,5 Seattle, WA 121.4 Chicago, IL 116.9 Richmond, VA 104.5 Phoenix, AZ 100,7 Detroit, MI 99.4 Atlanta, GA 95.6 932 Charlotto. Ne Dallas, TX 91.9 The table shown here gives a cost-of-living index for 14 different cities. Currently, you are living and working in Atlanta and earning $75000...

  • Western Airlines wants to design a hub system in the United States. Each hub is used...

    Western Airlines wants to design a hub system in the United States. Each hub is used for connecting flights to and from cities within 1500 miles of the hub. Western runs flights among the following cities: Atlanta, Boston, Chicago, Denver, Houston, Los Angeles, New Orleans, New York, Pittsburgh, Salt Lake City, San Francisco, and Seattle. The company wants to determine the smallest number of hubs it needs to cover all these cities. A city is covered if it is within...

  • TravelAir.com samples domestic airline flights to explore the relationship between airfare and distance. The service would...

    TravelAir.com samples domestic airline flights to explore the relationship between airfare and distance. The service would like to know if there is a correlation between airfare and flight distance. If there is a correlation, what percentage of the variation in airfare is accounted for by distance? How much does each additional mile add to the fare? The data follow. Distance Fare 636 $109 2,395 252 2,176 221 605 151 403 138 1,258 209 254 627 259 264 2,342 215 177...

  • Question 2: How do the average credit scores of people living in various cities in the...

    Question 2: How do the average credit scores of people living in various cities in the US differ? The file Credit Score Data of 143 American cities is provided in Canvas. Construct a histogram Create a Five Summary Report Calculate Mean, Variance and Standard Deviation   What conclusion can you reach concerning the average credit scores of people living in different American cities? City State Average Credit Score Detroit Mi 743 New York NY 762 Minneapolis MN 787 Hartford CT 774...

  • PYTHON Text file Seriesworld attached below and it is a list of teams that won from...

    PYTHON Text file Seriesworld attached below and it is a list of teams that won from 1903-2018. First time mentioned won in 1903 and last team mentioned won in 2018. World series wasn’t played in 1904 and 1994 and the file has entries that shows it. Write a python program which will read this file and create 2 dictionaries. The first key of the dictionary should show name of the teams and each keys value that is associated is the...

  • It's python. Please don't forget to print alpha-sorted states. Not random. Thank you // USPresdients.txt KY  ...

    It's python. Please don't forget to print alpha-sorted states. Not random. Thank you // USPresdients.txt KY   Abraham_Lincoln SC   Andrew_Jackson NC   Andrew_Johnson HI   Barack_Obama OH   Benjamin_Harrison AR   Bill_Clinton VT   Calvin_Coolidge VT   Chester_A_Arthur NY   Donald_Trump TX   Dwight_D_Eisenhower NY   Franklin_D_Roosevelt NH   Franklin_Pierce MA   George_H_W_Bush CT   George_W_Bush VA   George_Washington NE   Gerald_R_Ford NJ   Grover_Cleveland MO   Harry_S_Truman IA   Herbert_Hoover OH   James_A_Garfield PA   James_Buchanan NC   James_K_Polk VA   James_Madison VA   James_Monroe GA   Jimmy_Carter MA   John_Adams MA   John_F_Kennedy MA   John_Quincy_Adams VA   John_Tyler TX   Lyndon_B_Johnson NY   Martin_Van_Buren NY   Millard_Fillmore CA  ...

  • This will require a Linux operating system and regular expressions to complete. You can also use...

    This will require a Linux operating system and regular expressions to complete. You can also use https://regexr.com/ to find the appropriate regular expression to use on the text. Using the text from names.txt below, come up with an egrep command in the bash terminal that finds the lines requested. Using names.txt Find all entries of people who live in cities whose names are multiple words. Find all entries of people whose cities (if given) are at least 8 letters long....

  • 1. Calculate the mean and standard deviation for each variable using formulas or functions. 2. Calculate...

    1. Calculate the mean and standard deviation for each variable using formulas or functions. 2. Calculate descriptive statistics for each variable using the Analysis Toolpak. 3. Calculate the coefficient of variation for each variable. What general interpretation can you make from these values? 4. Calculate the correlation between Revenue and Employees using a formula. Calculate the coefficient of determination. 5. Create a correlation matrix for the eight numerical variables. Note any relationship of interest Rank COMPANY NAME City State Revenue...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT