Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

Stackorigin – The Community of Question and Answers

Stackorigin – The Community of Question and Answers Logo Stackorigin – The Community of Question and Answers Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
Home/Writing CSV files in Python

Writing CSV files in Python

  • Follow below steps to write data into a CSV file using Python
  • open the CSV file(example.csv) for writing (w mode) by using the open() function.
  • create a CSV writer object by calling the writer() function.
  • write data to CSV file by calling the writerow() / writerows()

writerow(): This method writes a single row at a time.

# writerow() Example
import csv
with open("emps.csv", "w", newline="") as f:
    data = csv.writer(f)
    header = ["eno", "ename", "emarks"]
    data.writerow(header)
    n = int(input("enter total emp's:"))
    for x in range(n):
        eno = input("enter eno:")
        ename = input("enter ename:")
        emarks = input("enter esal:")
        data.writerow([eno, ename, emarks])
    print("successfully store the data")

writerows(): This method is used to write multiple rows at a time.

# writerows() Example
import csv
with open("emps.csv", "w", newline="") as f:
    data = csv.writer(f)
    header = ["eno", "ename", "emarks"]
    data.writerow(header)
    n = int(input("enter total emp's:"))
    final_data = []
    for x in range(n):
        eno = input("enter eno:")
        ename = input("enter ename:")
        emarks = input("enter esal:")
        final_data.append([eno,ename,emarks])
    data.writerows(final_data)
    print("successfully store the data")
Share
  • Facebook

Sidebar

Ask A Question

Stats

  • Questions 1k
  • Answers 1k
  • Best Answers 80
  • Users 81

Adv 250x250

Adv 234x60

  • Recent
  • Answers
  • ashok

    How do I get the current time?

    • 2 Answers
  • ashok

    How to Install and Use Git on Windows

    • 1 Answer
  • ashok

    How to Check Git Version

    • 2 Answers
  • arjun

    Which institute is best for a data science course in ...

    • 1 Answer
  • ashok

    git stash and git pull

    • 1 Answer
  • pratap
    pratap added an answer How do I get the current time in Python: The time module… June 29, 2022 at 9:08 am
  • pratap
    pratap added an answer How do I get the current time in Python: Use datetime:… June 29, 2022 at 9:04 am
  • hari
    hari added an answer How to Install and Use Git on Windows Git is… June 29, 2022 at 8:24 am
  • sam
    sam added an answer How to Check Git Version: $ git --version git version… June 29, 2022 at 8:13 am
  • sam
    sam added an answer How to Check Git Version: In a command prompt: $… June 29, 2022 at 8:13 am

New Members

Sakshigupta1998

Sakshigupta1998

  • 0 Questions
  • 0 Answers
legal heir certificate online apply

legal heir certificate online apply

  • 0 Questions
  • 0 Answers
Mutual divorce process india

Mutual divorce process india

  • 0 Questions
  • 0 Answers
propertyregistration

propertyregistration

  • 0 Questions
  • 0 Answers
Best Gynecologist Hospital in Delhi

Best Gynecologist Hospital in Delhi

  • 0 Questions
  • 0 Answers

Adv 234x60

Trending Categories

Programming
825Questions
, 0Followers
Technology
209Questions
, 3Followers
General Knowledge
127Questions
, 0Followers
Business & Finance
81Questions
, 4Followers
Employment
73Questions
, 3Followers

Trending Tags

django (95) git (57) google (29) india (35) mysql (22) oil (54) pandas (23) python (241) usa (25) youtube (24)

Recent posts

    • On: June 29, 2022

    Bitbucket Cloud recently stopped supporting account passwords for Git authentication

    • On: June 21, 2022

    Best Telegram Movie Channels

    • On: June 17, 2022

    How to Open ICICI Bank Savings Account

    • On: June 15, 2022

    How to Download Your Own Twitch Videos

    • On: June 14, 2022

    How To Permanently Delete Your PSN Account

Explore Our Blog

Adv 234x60

Subscribe

Explore

  • Recent Questions
  • Most Answered
  • Answers
  • No Answers
  • Most Visited
  • Most Voted
  • Random

Footer

Stackorigin - The Community of Question and Answers

Stackorigin

Stackorigin is the world’s largest Q&A networking site, Stackorigin community brings you the collaboration of all the various Questions and the related Answers given by the community.

About

  • About Us
  • Contact Us
  • FAQ
  • Submit Guest Post Article on Technology, Education, Health, Apps, Gadgets, IoT, AI, Business, Digital Marketing and More

Info

  • Privacy Policy
  • Terms and Conditions
  • Community Guidelines
  • Tags

Products

  • Tutorials
  • Advertising
  • Categories
  • Corona
  • StackHow

Follow

© 2022 Stackorigin. All Rights Reserved.