Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the dataset about animal statistics. Where the cat_teeth, dog_teeth, horse_teeth, and these numfeet variables are all the integers.

            print(“Cat”,sum(cat_teeth), cat_numfeet)

            print(“Dog”,sum(dog_teeth), dog_numfeet)

            print(“Horse”,sum(horse_teeth), horse_numfeet)

The output I am getting is:

Cat 38 4jj

Dog 21 4

Horse 28 4

I want that same output exported to the csv file where I have 3 columns as shown above delimited by the comma (,).

How do I achieve it?

import csv 

    with open(“results.csv”, “w”) as csvfile:

    writer= csv.writer(csvfile)

    writer.writerow(“Cat”,sum(cat_teeth), cat_numfeet))

    writer.writerow(“Dog”,sum(dog_teeth), dog_numfeet)     

    writer.writerow(“Horse”,sum(horse_teeth), horse_numfeet)

This is not working.

1 Answer

0 votes
by (36.8k points)

The writerow takes only one argument. Convert these values to the list and then call this function.

writer.writerow(["Cat",sum(cat_teeth), cat_numfeet])

Want to gain skills in Data Science with Python? Sign up today for this Python for Data Science Course and be a master in it 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...