-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.py
108 lines (63 loc) · 3.19 KB
/
sorting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#Write a program that reads in GBplaces.csv and write the data out to a new file in the same format,
#but with the places ordered by increasing population size. (Assigment week 4)
def openfile(string, mode):
# checks if file can be opened
# returns true if it can be
# takes two string as argument
try:
file = open(string, mode)
file.close()
return True
except:
print("File " + string + " cannot be opened!")
return False
def isnumber(arg):
# function used to check the input whether it is convertible to float or not
try:
float(arg)
return True
except:
return False
def sortingpopulation(file):
# takes a reference to a file with text
# sorts the lines in ascending order according to the second column if the column contains numbers
# returns the sorted array if there is an error it returns an empty array
lines = [] # array made to contain lines
for line in file:
linearray = line.split(",") # splitting the line
lines.append(linearray) # we add the line to out array
for i in lines:
if(i[0] == "% place"):
lines.pop(lines.index(i)) # we remove the first element it contains no data
for x in lines: # checking each line if it contains numbers in the population
if not isnumber(x[2]):
print("The population contains non integer elements.")
print(x)
return []
return sorted(lines, key=lambda x: float(x[2])) # we use the sorting function and we give the lines as input
# then we give a function(lambda) that converts every second element of the line to a float we could convert to
# integer too if we want
def main():
# main function
# calls a function to check if the file can be accessed or not
if not openfile("GBplaces.csv", "r"): # checking if GBplaces.csv can be opened if not return 0
return 0
file = open("GBplaces.csv", "r") # opening GBplaces.csv
linessorted = sortingpopulation(file) # sorting the lines inside the file and saving them in array
if linessorted == []: # if the array is empty we say that and return 0
print("The population could not be sorted.")
return 0
if not openfile("GBplacesSorted.csv", "w+"): # checking if GBplacesSorted.csv can be opened if not return 0
return 0
filesorted = open("GBplacesSorted.csv", "w+") # opening GBplacesSorted.csv
finalarray = [['% place', 'type', 'population', 'latitude', 'longitude\n']] # this the first line from the orginal
# format we want to keep the ordinal format so it is added as the first element
finalarray.extend(linessorted) # we add the sorted lines to the array
for i in finalarray: # writing the sorted array to the file using the same format it was written in
filesorted.write(i[0] + "," + i[1] + "," + str(i[2]) + "," + i[3] + "," + i[4])
# we close the files
filesorted.close()
file.close()
print("The result is in GBplacesSorted.csv.")
return 0
main() # calling the main function