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"))

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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"))

coat_pattern
Ticked tabby
Multi-color
Semi-long
Semi-long
Foreign Semi-long
All
Rex
All
Natural Moderate and muscular Short Aly
Short Evenly solid
Asian Natural Moderate
Asian Semi-longhair Crossbreed between the Burmese and long-haired cats
Australian Mist Crossbreed between the Abyssinian and Burmese Moderate
Balinese
Mutation of the Siamese Semi-foreign Long
Bambino Crossbreed between the Munchkin and Sphynx
Dwarf
Birman The original Birman was crossed with the Siamese and the Persian to create the
Bombay Crossbreed between the Black American Shorthair and Sable Burmese
Brazilian Shorthair
Colorpoint
Rex
Cobby
Natural Normal Short All
British Longhair
Natural Cobby Semi-long
Natural Cobby Short All
Burmese Natural Semi-foreign or semi-cobby.
British Shorthair
Burmilla
California Spangled
breed type body_type
Abyssinian
Aegean Natural Moderate
American Bobtail
American Curl Mutation
American Ringtail
American Shorthair
American Wirehair
Aphrodite Giant Natural Lean and muscular
Arabian Mau
coat_type
Semi-long
Semi-foreign
Natural Semi-foreign Short
Cobby
Mutation
Mutation
Natural Cobby Short
Mutation
Normal
Semi-foreign
All
Short
Cobby Long
Oriental
All
All
All
Natural Moderate
All
All
All
Short Solid or Tortoiseshell
Crossbreed between the Brown Tortie Burmese and the Chinchilla Persian Semi-cobby
"Crossbreed between the Abyssinian, American Shorthair and British Shorthair"
Natural Cobby Long "Solid, classic tabby, spotted tabby and ticked tabby"
Comes in varying shades of blue
Abyssinian, Siamese and short-haired cats"
All
Mutation of the Manx
All
Rex
Foreign Short
All
Semi-cobby
Long
York Chocolate Natural Moderate
Cobby
Short
All
Short Solid reddish-brown
"Thai orTraditional, Classic, or Old-style Siamese; Wichien Maat"
"Thai Lilac, Thai Blue Point and Thai Lilac Point"
Tonkinese
Crossbreed between the Burmese and Siamese
Toybob Mutation
Dwarf Short all
Toyger Crossbreed/hybrid between the Bengal and short-haired cats
All
Turkish Angora Natural Semi-cobby
Semi-long
Semi-long
Turkish Van
Natural Semi-cobby
Turkish Vankedisi
Van pattern
Natural Svelte Long Solid white
Crossbreed between the Donskoy and Scottish Fold
Long
Ukrainian Levkoy
Solid
Dwarf Hairless
Chantilly-Tiffany
Chartreux
Natural Muscular; cobby Short
Colorpoint Shorthair "Crossbreed between the
Cornish Rex Mutation
Foreign Rex
"Cymric, Manx Longhair or Long-haired Manx"
Cyprus Natural Lean and muscular
All
Devon Rex
Mutation
Donskoy orDon Sphynx Mutation
Dragon Li orChinese Li Hua
All
Hairless
Ticked tabby
and Sphynx"
Spotted tabby
Semi-foreign
Natural Normal Short
Dwelf "Crossbreed between the American Curl, Munchkin
Egyptian Mau Natural Moderate and muscular Short
European Shorthair Natural Moderate
Short All
Crossbreed between the American Shorthair and Persian
Foldex Crossbreed between the Exotic Shorthair and Scottish Fold
Cobby
German Rex
Mutation
Semi-foreign Rex All
Crossbreed between the Siamese and black short-haired cats
Crossbreed between the Desert Lynx and Jungle Curl
Exotic Shorthair
Havana Brown
Highlander
All
Himalayan orColorpoint Persian Crossbreed between the Persian and Siamese
Japanese Bobtail
Mutation
Moderate
Short/long
Javanese orColorpoint Longhair "Crossbreed between the Balinese (with some Colorpoint Shorthair), Oriental Longhair and Siamese"
Khao Manee Natural Moderate
Short Solid white
Kinkalow
Crossbreed between the Munchkin and American Curl
Korat Natural
Korean Bobtail
Semi-foreign or semi-cobby and muscular Short Solid blue
"Natural, mutation" Moderate
Short/long
Mutation
All
"Natural, crossbreed"
Moderate
Short
Korn Ja Natural Small Short Solid black
Kurilian Bobtail orkuril Islands Bobtail
"Natural, mutation"
Lambkin Crossbreed between the Munchkin and Selkirk Rex Dwarf Rex
LaPerm Mutation
Moderate
Rex All
Lykoi
Moderate
Hairless
Solid black roan
Maine Coon
Large Semi-long to long
Manx Mutation
Short/long
All
Mekong Bobtail Mutation
Moderate
Colorpoint
Minskin Crossbreed between the Munchkin and Sphynx Dwarf Hairless
Minuet Crossbreed between the Persian and Munchkin Dwarf Short/Long
Munchkin
Mutation
Dwarf Short/long
All
Nebelung
"Natural, mutation". Foreign Semi-long
Norwegian Forest Cat Natural Cobby Long All but colorpoint
Ocicat "Crossbreed between the Abyssinian, American Shorthair and Siamese"
Ojos Azules Crossbreed
Oregon Rex (extinct)
Oriental Bicolor
Solid blue
Moderate
Short All
Rex
Mutation
Color variety of the Oriental Shorthair Oriental
Short Bicolor
Oriental
Oriental Longhair
Oriental Shorthair
Persian (modern)
Persian (traditional)
Peterbald
Pixie-bob
Crossbreed between the Oriental Shorthair and long-haired cats
Crossbreed between the European Shorthair and Siamese Oriental
Mutation of the Traditional Persian Cobby Long All but colorpoint
"Natural, but some crossbreeding with the Turkish Angora"
Cobby Long All but colorpoint
"Crossbreed between the Donskoy, Oriental Shorthair and Siamese; before this, it was between the Balinese and Javanese" Oriental
Mutation (falsely claimed to be a bobcat hybrid early on)
Medium Short Spotted tabby
Ragamuffin orLiebling (obsolete)
"Crossbreed between the Ragdoll with limited out-crossing to the Himalayan, the Persian, and other long-haired cats" Cobby Long All
Ragdoll "Behavioral mutation in a crossbreed, presumed to be between the Persian or Turkish Angora and the Birman or Burmese" Cobby Long "Colorpoint, mitted, or bicolor"
Raas Natural Moderate
Short "Solid blue, solid cinnamon, or cinnamon colorpoint"
Russian Blue Natural "Moderate, Oriental" Short Solid blue
"Russian White, Russian Black and Russian Tabby"
"Crossbreeds between the Russian Blue and short-haired cats from Siberia, Russia"
Sam Sawet
Color variety of the Thai
Moderate
Short Solid
Scottish Fold Mutation of the bones and cartilage of the ears Cobby Short/long
All
Selkirk Rex "Mutation/crossbreed between the American Shorthair, Persian, Himalayan, Exotic Shorthair and British Shorthair"
Oriental
Short Spotted
appear to be semi-long)"
Crossbreed/hybrid between the Bengal and Oriental Shorthair
Serengeti
Serrade Petit Natural Semi-cobby
Short "Solid tan, solid orange and solid white"
"Siamese (modern) (for traditional, see Thai below)" Mutation of the Thai Oriental
Siberian orSiberian Forest Cat; Neva Masquerade (colorpoint variety) Natural Semi-cobby
Singapura
"Possibly a mutation of a crossbreed (excluding the Munchkin), solving why they're so small"
Crossbreed between the American Shorthair and Siamese Moderate
Short Mitted colorpoint
Short Colorpoint
Semi-long
Snowshoe
Sokoke Natural Moderate
Somali Mutation
Ticked tabby
Ticked tabby
Hairless
Sphynx Mutation
Suphalak
Moderate
Semi-long
Spotted or marbled
Birman of today. Cobby
Short Solid black
Cobby Short All
Short All
Semi-foreign
Colorpoint
Semi-cobby
All
Dwarf Short All
All
All
Moderate
All
Evenly solid
Short/long
Cobby Long Colorpoint
Short/long
Natural Moderate
Color varieties of the Korat
Oriental
Short
Short
Moderate
Short
Semi-long
Short Solid brown
All
Large Short Spotted tabby
All
Solid with Shaded Silver and Silver Tipped patterns
Short Spotted tabby
Colorpoint
Semi-long
All
Mitted colorpoint
All
Small
Oriental
cats.txt
Moderate
Hairless
Solid gray
"Solid chocolate, solid lilac and solid taupe or any of these colors with white"
"All; if colorpoint is considered to be a separate breed, it is called the Javanese"
Short Ticked tabby
Long Colorpoint
"Hairless, velour, brush, or straight coat" All
Moderate
Short "Solid white, solid black and tabby"
Large and cobby "Short/long (longhair, sometimes in early generations, can
Short Colorpoint
Moderate
Short Solid lilac and colorpoint (blue point and lilac point only)
"Colorpoint, mink, or solid"
Short Mackerel tabby
Transcribed Image Text:coat_pattern Ticked tabby Multi-color Semi-long Semi-long Foreign Semi-long All Rex All Natural Moderate and muscular Short Aly Short Evenly solid Asian Natural Moderate Asian Semi-longhair Crossbreed between the Burmese and long-haired cats Australian Mist Crossbreed between the Abyssinian and Burmese Moderate Balinese Mutation of the Siamese Semi-foreign Long Bambino Crossbreed between the Munchkin and Sphynx Dwarf Birman The original Birman was crossed with the Siamese and the Persian to create the Bombay Crossbreed between the Black American Shorthair and Sable Burmese Brazilian Shorthair Colorpoint Rex Cobby Natural Normal Short All British Longhair Natural Cobby Semi-long Natural Cobby Short All Burmese Natural Semi-foreign or semi-cobby. British Shorthair Burmilla California Spangled breed type body_type Abyssinian Aegean Natural Moderate American Bobtail American Curl Mutation American Ringtail American Shorthair American Wirehair Aphrodite Giant Natural Lean and muscular Arabian Mau coat_type Semi-long Semi-foreign Natural Semi-foreign Short Cobby Mutation Mutation Natural Cobby Short Mutation Normal Semi-foreign All Short Cobby Long Oriental All All All Natural Moderate All All All Short Solid or Tortoiseshell Crossbreed between the Brown Tortie Burmese and the Chinchilla Persian Semi-cobby "Crossbreed between the Abyssinian, American Shorthair and British Shorthair" Natural Cobby Long "Solid, classic tabby, spotted tabby and ticked tabby" Comes in varying shades of blue Abyssinian, Siamese and short-haired cats" All Mutation of the Manx All Rex Foreign Short All Semi-cobby Long York Chocolate Natural Moderate Cobby Short All Short Solid reddish-brown "Thai orTraditional, Classic, or Old-style Siamese; Wichien Maat" "Thai Lilac, Thai Blue Point and Thai Lilac Point" Tonkinese Crossbreed between the Burmese and Siamese Toybob Mutation Dwarf Short all Toyger Crossbreed/hybrid between the Bengal and short-haired cats All Turkish Angora Natural Semi-cobby Semi-long Semi-long Turkish Van Natural Semi-cobby Turkish Vankedisi Van pattern Natural Svelte Long Solid white Crossbreed between the Donskoy and Scottish Fold Long Ukrainian Levkoy Solid Dwarf Hairless Chantilly-Tiffany Chartreux Natural Muscular; cobby Short Colorpoint Shorthair "Crossbreed between the Cornish Rex Mutation Foreign Rex "Cymric, Manx Longhair or Long-haired Manx" Cyprus Natural Lean and muscular All Devon Rex Mutation Donskoy orDon Sphynx Mutation Dragon Li orChinese Li Hua All Hairless Ticked tabby and Sphynx" Spotted tabby Semi-foreign Natural Normal Short Dwelf "Crossbreed between the American Curl, Munchkin Egyptian Mau Natural Moderate and muscular Short European Shorthair Natural Moderate Short All Crossbreed between the American Shorthair and Persian Foldex Crossbreed between the Exotic Shorthair and Scottish Fold Cobby German Rex Mutation Semi-foreign Rex All Crossbreed between the Siamese and black short-haired cats Crossbreed between the Desert Lynx and Jungle Curl Exotic Shorthair Havana Brown Highlander All Himalayan orColorpoint Persian Crossbreed between the Persian and Siamese Japanese Bobtail Mutation Moderate Short/long Javanese orColorpoint Longhair "Crossbreed between the Balinese (with some Colorpoint Shorthair), Oriental Longhair and Siamese" Khao Manee Natural Moderate Short Solid white Kinkalow Crossbreed between the Munchkin and American Curl Korat Natural Korean Bobtail Semi-foreign or semi-cobby and muscular Short Solid blue "Natural, mutation" Moderate Short/long Mutation All "Natural, crossbreed" Moderate Short Korn Ja Natural Small Short Solid black Kurilian Bobtail orkuril Islands Bobtail "Natural, mutation" Lambkin Crossbreed between the Munchkin and Selkirk Rex Dwarf Rex LaPerm Mutation Moderate Rex All Lykoi Moderate Hairless Solid black roan Maine Coon Large Semi-long to long Manx Mutation Short/long All Mekong Bobtail Mutation Moderate Colorpoint Minskin Crossbreed between the Munchkin and Sphynx Dwarf Hairless Minuet Crossbreed between the Persian and Munchkin Dwarf Short/Long Munchkin Mutation Dwarf Short/long All Nebelung "Natural, mutation". Foreign Semi-long Norwegian Forest Cat Natural Cobby Long All but colorpoint Ocicat "Crossbreed between the Abyssinian, American Shorthair and Siamese" Ojos Azules Crossbreed Oregon Rex (extinct) Oriental Bicolor Solid blue Moderate Short All Rex Mutation Color variety of the Oriental Shorthair Oriental Short Bicolor Oriental Oriental Longhair Oriental Shorthair Persian (modern) Persian (traditional) Peterbald Pixie-bob Crossbreed between the Oriental Shorthair and long-haired cats Crossbreed between the European Shorthair and Siamese Oriental Mutation of the Traditional Persian Cobby Long All but colorpoint "Natural, but some crossbreeding with the Turkish Angora" Cobby Long All but colorpoint "Crossbreed between the Donskoy, Oriental Shorthair and Siamese; before this, it was between the Balinese and Javanese" Oriental Mutation (falsely claimed to be a bobcat hybrid early on) Medium Short Spotted tabby Ragamuffin orLiebling (obsolete) "Crossbreed between the Ragdoll with limited out-crossing to the Himalayan, the Persian, and other long-haired cats" Cobby Long All Ragdoll "Behavioral mutation in a crossbreed, presumed to be between the Persian or Turkish Angora and the Birman or Burmese" Cobby Long "Colorpoint, mitted, or bicolor" Raas Natural Moderate Short "Solid blue, solid cinnamon, or cinnamon colorpoint" Russian Blue Natural "Moderate, Oriental" Short Solid blue "Russian White, Russian Black and Russian Tabby" "Crossbreeds between the Russian Blue and short-haired cats from Siberia, Russia" Sam Sawet Color variety of the Thai Moderate Short Solid Scottish Fold Mutation of the bones and cartilage of the ears Cobby Short/long All Selkirk Rex "Mutation/crossbreed between the American Shorthair, Persian, Himalayan, Exotic Shorthair and British Shorthair" Oriental Short Spotted appear to be semi-long)" Crossbreed/hybrid between the Bengal and Oriental Shorthair Serengeti Serrade Petit Natural Semi-cobby Short "Solid tan, solid orange and solid white" "Siamese (modern) (for traditional, see Thai below)" Mutation of the Thai Oriental Siberian orSiberian Forest Cat; Neva Masquerade (colorpoint variety) Natural Semi-cobby Singapura "Possibly a mutation of a crossbreed (excluding the Munchkin), solving why they're so small" Crossbreed between the American Shorthair and Siamese Moderate Short Mitted colorpoint Short Colorpoint Semi-long Snowshoe Sokoke Natural Moderate Somali Mutation Ticked tabby Ticked tabby Hairless Sphynx Mutation Suphalak Moderate Semi-long Spotted or marbled Birman of today. Cobby Short Solid black Cobby Short All Short All Semi-foreign Colorpoint Semi-cobby All Dwarf Short All All All Moderate All Evenly solid Short/long Cobby Long Colorpoint Short/long Natural Moderate Color varieties of the Korat Oriental Short Short Moderate Short Semi-long Short Solid brown All Large Short Spotted tabby All Solid with Shaded Silver and Silver Tipped patterns Short Spotted tabby Colorpoint Semi-long All Mitted colorpoint All Small Oriental cats.txt Moderate Hairless Solid gray "Solid chocolate, solid lilac and solid taupe or any of these colors with white" "All; if colorpoint is considered to be a separate breed, it is called the Javanese" Short Ticked tabby Long Colorpoint "Hairless, velour, brush, or straight coat" All Moderate Short "Solid white, solid black and tabby" Large and cobby "Short/long (longhair, sometimes in early generations, can Short Colorpoint Moderate Short Solid lilac and colorpoint (blue point and lilac point only) "Colorpoint, mink, or solid" Short Mackerel tabby
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.

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY