Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # GBTownsSortedByIncreasingPopulation
- # Reads and sorts data in order of increasing population
- # Writes this data to a seperate file
- # Empty arrays for file
- place = [];
- townorcity = [];
- population = [];
- latitude = [];
- longitude = [];
- opened = 0
- try:
- readFile = open('GBplaces.csv','r');
- opened = 1
- except:
- print('Could not open file.');
- # Skips headings of file
- next(readFile);
- # Using this as a check to see if the correct file is opened
- # Also used to see if it has sorted in right order when written
- if opened:
- for line in readFile:
- splitUp = line.split(',');
- place.append(splitUp[0]);
- townorcity.append(splitUp[1]);
- population.append(int(splitUp[2]));
- latitude.append(splitUp[3]);
- longitude.append(splitUp[4]);
- print(splitUp[0] + ',' + splitUp[1] + ',' + splitUp[2] + ',' + splitUp[3] + ',' + splitUp[4]);
- readFile.close();
- # Write a file which is sorted in population ascending order
- writeFile = open('GBplacesSorted.csv','w');
- sortpopulation = sorted(population);
- sortedplace = [place for (population,place) in sorted(zip(population,place), key=lambda pair: pair[0])];
- sortedtownorcity = [townorcity for (population, townorcity) in sorted(zip(population, townorcity), key=lambda pair: pair[0])];
- sortedlatitude = [latitude for (population,latitude) in sorted(zip(population, latitude), key=lambda pair: pair[0])];
- sortedlongitude = [longitude for (population, longitude) in sorted(zip(population, longitude), key=lambda pair: pair[0])];
- for i in range(0,len(place)):
- writeFile.write(sortedplace[i] + ',' + str(sortpopulation[i]) + ',' + str(sortedtownorcity[i]) + ',' + str(sortedlatitude[i]) + ',' + str(sortedlongitude[i]));
- writeFile.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement