python: def txt_to_csv(input_file): """ Question 3 - cats.txt contains information about cat breeds and their various characteristics. - Read the cats.txt with FILE I/O. - These are TAB separated values - Use a list of lists to create a CSV file with the appropriate data WITHOUT WHITESPACE (remove \t) named "cleaned_cats.csv". - Exclude the "body_type" column - If a row has a length < 5, remove it. - For strings containing extra quotes, strip the extra quotes. - Return the list of lists. Args: input_file (cats.txt) Returns: List of lists Output: [['breed', 'type', 'coat_type', 'coat_pattern'], ['Abyssinian', 'Natural', 'Short', 'Ticked tabby'], ['Aegean', 'Natural', 'Semi-long', 'Multi-color'], ['American Bobtail', 'Mutation', 'Semi-long', 'All'], ['American Curl', 'Mutation', 'Semi-long', 'All'], ['American Ringtail', 'Mutation', 'Semi-long', 'All'], ['American Shorthair', 'Natural', 'Short', 'All'], ... ['Ukrainian Levkoy', 'Crossbreed between the Donskoy and Scottish Fold', 'Hairless', 'Solid gray'], ['York Chocolate', 'Natural', 'Long', 'Solid chocolate, solid lilac and solid taupe or any of these colors with white']] Length: 96 """ print(txt_to_csv("cats.txt"))
python: def txt_to_csv(input_file): """ Question 3 - cats.txt contains information about cat breeds and their various characteristics. - Read the cats.txt with FILE I/O. - These are TAB separated values - Use a list of lists to create a CSV file with the appropriate data WITHOUT WHITESPACE (remove \t) named "cleaned_cats.csv". - Exclude the "body_type" column - If a row has a length < 5, remove it. - For strings containing extra quotes, strip the extra quotes. - Return the list of lists. Args: input_file (cats.txt) Returns: List of lists Output: [['breed', 'type', 'coat_type', 'coat_pattern'], ['Abyssinian', 'Natural', 'Short', 'Ticked tabby'], ['Aegean', 'Natural', 'Semi-long', 'Multi-color'], ['American Bobtail', 'Mutation', 'Semi-long', 'All'], ['American Curl', 'Mutation', 'Semi-long', 'All'], ['American Ringtail', 'Mutation', 'Semi-long', 'All'], ['American Shorthair', 'Natural', 'Short', 'All'], ... ['Ukrainian Levkoy', 'Crossbreed between the Donskoy and Scottish Fold', 'Hairless', 'Solid gray'], ['York Chocolate', 'Natural', 'Long', 'Solid chocolate, solid lilac and solid taupe or any of these colors with white']] Length: 96 """ print(txt_to_csv("cats.txt"))
python: def txt_to_csv(input_file): """ Question 3 - cats.txt contains information about cat breeds and their various characteristics. - Read the cats.txt with FILE I/O. - These are TAB separated values - Use a list of lists to create a CSV file with the appropriate data WITHOUT WHITESPACE (remove \t) named "cleaned_cats.csv". - Exclude the "body_type" column - If a row has a length < 5, remove it. - For strings containing extra quotes, strip the extra quotes. - Return the list of lists. Args: input_file (cats.txt) Returns: List of lists Output: [['breed', 'type', 'coat_type', 'coat_pattern'], ['Abyssinian', 'Natural', 'Short', 'Ticked tabby'], ['Aegean', 'Natural', 'Semi-long', 'Multi-color'], ['American Bobtail', 'Mutation', 'Semi-long', 'All'], ['American Curl', 'Mutation', 'Semi-long', 'All'], ['American Ringtail', 'Mutation', 'Semi-long', 'All'], ['American Shorthair', 'Natural', 'Short', 'All'], ... ['Ukrainian Levkoy', 'Crossbreed between the Donskoy and Scottish Fold', 'Hairless', 'Solid gray'], ['York Chocolate', 'Natural', 'Long', 'Solid chocolate, solid lilac and solid taupe or any of these colors with white']] Length: 96 """ print(txt_to_csv("cats.txt"))
def txt_to_csv(input_file): """ Question 3 - cats.txt contains information about cat breeds and their various characteristics. - Read the cats.txt with FILE I/O. - These are TAB separated values - Use a list of lists to create a CSV file with the appropriate data WITHOUT WHITESPACE (remove \t) named "cleaned_cats.csv". - Exclude the "body_type" column - If a row has a length < 5, remove it. - For strings containing extra quotes, strip the extra quotes. - Return the list of lists.
Args: input_file (cats.txt) Returns: List of lists
Output: [['breed', 'type', 'coat_type', 'coat_pattern'], ['Abyssinian', 'Natural', 'Short', 'Ticked tabby'], ['Aegean', 'Natural', 'Semi-long', 'Multi-color'], ['American Bobtail', 'Mutation', 'Semi-long', 'All'], ['American Curl', 'Mutation', 'Semi-long', 'All'], ['American Ringtail', 'Mutation', 'Semi-long', 'All'], ['American Shorthair', 'Natural', 'Short', 'All'], ... ['Ukrainian Levkoy', 'Crossbreed between the Donskoy and Scottish Fold', 'Hairless', 'Solid gray'], ['York Chocolate', 'Natural', 'Long', 'Solid chocolate, solid lilac and solid taupe or any of these colors with white']]
Length: 96 """
print(txt_to_csv("cats.txt"))
Expert Solution
Overview
In this question we have to write a python function that reads data from a tab-separated text file, cleans the data, and writes it to a CSV file. The function takes the name of the text file as an input and returns the cleaned data as a list of lists. The code implements the requirements specified in the question, such as excluding the "body_type" column, removing rows with a length less than 5, and stripping extra quotes from strings.
Let's code, hope this helps. You may use threaded question feature to ask your further queries if any.