Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. def process_csv_line( line ):
  4. # We want to remove empty spaces and line breaks from the
  5. # beginning and end of each line
  6. line = line.strip()
  7. # We want to remove double quotes '"' from the text, for
  8. # that we use the "replace" function which takes here two
  9. # parameters, the first one is the character to replace
  10. # and the second one is the replacement.
  11. line = line.replace( '"', '' )
  12. # We want to split each line based on the separator ";".
  13. line = line.split( ";" )
  14. return line
  15.  
  16. # Here I define a function that loads the content
  17. # of a file in memory, given a filename.
  18. # It takes a parameter, the variable "filename",
  19. # it returns a list containing the content of
  20. # the file corresponding to "filename".
  21. def load_content_of_file_in_list( filename ):
  22. # here is a declared variable, of type list
  23. # for the moment this variable is empty
  24. content_file = []
  25. # Here I open the file with the filename given
  26. # as parameter and with the mode "r" which means
  27. # read only. I declare the variable "file_csv" as
  28. # my file handler (or wrapper).
  29. with open( filename, mode = "r" ) as file_csv:
  30. for line in file_csv:
  31. # Here i call the function "process_csv_line"
  32. # which contains all the code dedicated to
  33. # processing one line at a time from my file.
  34. line = process_csv_line( line )
  35. # Here I add the content of the variable "line" to the variable
  36. # "content_file"
  37. content_file.append( line )
  38. return content_file
  39.  
  40. # This function is dedicated to testing the size (or length)
  41. # of a list.
  42. # It receives a list "my_list" as parameter and
  43. # it returns a string "message".
  44. def test_length_list( my_list ):
  45. if len( my_list ) <= 0:
  46. message = "Error: the file is empty!"
  47. else:
  48. message = "Success: the file was loaded in memory!"
  49. return message
  50.  
  51. def get_column_position( name, column_names ):
  52. return column_names.index( name )
  53.  
  54. # Here is the function answering question 1
  55. # it takes a parameter, the variable "filename"
  56. # it returns a list containing the content of
  57. # the file corresponding to "filename".
  58. def question_1_function( filename ):
  59. content_file = load_content_of_file_in_list( filename )
  60. # Here I test the length of the list "content_file"
  61. # and print two different messages corresponding to
  62. # an empty or non empty list situation respectively.
  63. print( test_length_list( content_file ) )
  64. # Here I return, as the result of the function, the variable
  65. # "content_file", if it is empty or not.
  66. return content_file
  67.  
  68. # Here is the function answering question 2
  69. # it takes two parameters: the content of a file
  70. # processed according to question 1 and a country
  71. # name,
  72. # it returns the population for the country name
  73. # given as parameter.
  74. def question_2_function( content, country_name ):
  75. # First, we loop over the countries contained
  76. # in the list "content".
  77. for country in content:
  78. # Second, we temporarily store the first element
  79. # of every list "country" in a variable "temporary_
  80. # countr_name".
  81. temporary_country_name = country[ 0 ]
  82. # Third, we compare the temporary country name to
  83. # the country name give as function parameter.
  84. if country_name == temporary_country_name:
  85. # If the country names match, we return the third
  86. # element of the list "country", which is the population.
  87. return country[ 2 ]
  88.  
  89. # Here is the function answering question 3.
  90. # It takes two parameters, the first one is the content
  91. # of the file to process, the second one is the name of the
  92. # column we want to extract.
  93. # It returns the extracted column as a list.
  94. def question_3_function( content, column_name ):
  95. # First, we store the header of the file in a variable
  96. # called "header".
  97. header = content[ 0 ]
  98. # Second, we retrieve the position of the column we
  99. # want to extract based on the column name.
  100. position = get_column_position( column_name, header )
  101. column = []
  102. # Third, we loop over each list "country", extract the
  103. # element at the wanted "position" and store it in the
  104. # list "column".
  105. for country in content:
  106. column.append( country[ position ] )
  107. return column
  108.  
  109. # Here I declared a variable which contains the absolute
  110. # path of the CSV file I want to process.
  111. file_absolute_path = "/home/student/Documents/20190114/eu_countries.csv"
  112.  
  113. # Here I call the function answering Question 1
  114. # I give a parameter which is the absolute file path to process
  115. content_file_csv = question_1_function( file_absolute_path )
  116. print( content_file_csv )
  117.  
  118. population = question_2_function( content_file_csv, "Germany" )
  119. print( population )
  120.  
  121. #'Country', 'Capital', 'Population (approx.)', 'Official Language(s)'
  122. column = question_3_function( content_file_csv, "Official Language(s)" )
  123. print( column )
  124.  
  125. user_input = input( "Enter a country name: " )
  126. print( "The population for the country", user_input, "is: ", question_2_function( content_file_csv, user_input ) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement