Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- def process_csv_line( line ):
- # We want to remove empty spaces and line breaks from the
- # beginning and end of each line
- line = line.strip()
- # We want to remove double quotes '"' from the text, for
- # that we use the "replace" function which takes here two
- # parameters, the first one is the character to replace
- # and the second one is the replacement.
- line = line.replace( '"', '' )
- # We want to split each line based on the separator ";".
- line = line.split( ";" )
- return line
- # Here I define a function that loads the content
- # of a file in memory, given a filename.
- # It takes a parameter, the variable "filename",
- # it returns a list containing the content of
- # the file corresponding to "filename".
- def load_content_of_file_in_list( filename ):
- # here is a declared variable, of type list
- # for the moment this variable is empty
- content_file = []
- # Here I open the file with the filename given
- # as parameter and with the mode "r" which means
- # read only. I declare the variable "file_csv" as
- # my file handler (or wrapper).
- with open( filename, mode = "r" ) as file_csv:
- for line in file_csv:
- # Here i call the function "process_csv_line"
- # which contains all the code dedicated to
- # processing one line at a time from my file.
- line = process_csv_line( line )
- # Here I add the content of the variable "line" to the variable
- # "content_file"
- content_file.append( line )
- return content_file
- # This function is dedicated to testing the size (or length)
- # of a list.
- # It receives a list "my_list" as parameter and
- # it returns a string "message".
- def test_length_list( my_list ):
- if len( my_list ) <= 0:
- message = "Error: the file is empty!"
- else:
- message = "Success: the file was loaded in memory!"
- return message
- def get_column_position( name, column_names ):
- return column_names.index( name )
- # Here is the function answering question 1
- # it takes a parameter, the variable "filename"
- # it returns a list containing the content of
- # the file corresponding to "filename".
- def question_1_function( filename ):
- content_file = load_content_of_file_in_list( filename )
- # Here I test the length of the list "content_file"
- # and print two different messages corresponding to
- # an empty or non empty list situation respectively.
- print( test_length_list( content_file ) )
- # Here I return, as the result of the function, the variable
- # "content_file", if it is empty or not.
- return content_file
- # Here is the function answering question 2
- # it takes two parameters: the content of a file
- # processed according to question 1 and a country
- # name,
- # it returns the population for the country name
- # given as parameter.
- def question_2_function( content, country_name ):
- # First, we loop over the countries contained
- # in the list "content".
- for country in content:
- # Second, we temporarily store the first element
- # of every list "country" in a variable "temporary_
- # countr_name".
- temporary_country_name = country[ 0 ]
- # Third, we compare the temporary country name to
- # the country name give as function parameter.
- if country_name == temporary_country_name:
- # If the country names match, we return the third
- # element of the list "country", which is the population.
- return country[ 2 ]
- # Here is the function answering question 3.
- # It takes two parameters, the first one is the content
- # of the file to process, the second one is the name of the
- # column we want to extract.
- # It returns the extracted column as a list.
- def question_3_function( content, column_name ):
- # First, we store the header of the file in a variable
- # called "header".
- header = content[ 0 ]
- # Second, we retrieve the position of the column we
- # want to extract based on the column name.
- position = get_column_position( column_name, header )
- column = []
- # Third, we loop over each list "country", extract the
- # element at the wanted "position" and store it in the
- # list "column".
- for country in content:
- column.append( country[ position ] )
- return column
- # Here I declared a variable which contains the absolute
- # path of the CSV file I want to process.
- file_absolute_path = "/home/student/Documents/20190114/eu_countries.csv"
- # Here I call the function answering Question 1
- # I give a parameter which is the absolute file path to process
- content_file_csv = question_1_function( file_absolute_path )
- print( content_file_csv )
- population = question_2_function( content_file_csv, "Germany" )
- print( population )
- #'Country', 'Capital', 'Population (approx.)', 'Official Language(s)'
- column = question_3_function( content_file_csv, "Official Language(s)" )
- print( column )
- user_input = input( "Enter a country name: " )
- 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