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 a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

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

Adv 120x600

Looking for advertising?

Consider These Things If You Appreciate What stackorigin Does: Stackorigin is a community where you can ask questions and find answers, for free to everyone. We would appreciate it if you bought us a coffee if you liked what we were doing.

Buy me a Coffe

Adv 120x600

  • Random
  • Answers
    • On: August 3, 2022
    • Answers: 0

    order generic Spiriva France, Elephant in spiriva ad

    • On: July 6, 2021
    • Answer: 1

    You have ad crawler errors, which can result in lost ...

    • On: February 21, 2022
    • Answer: 1

    How to Block SBI Debit Card

    • On: March 15, 2021
    • Answers: 0

    What is the use of 20 DP Schneider switch?

    • On: December 30, 2020
    • Answer: 1

    Who has the most subscribers on YouTube 2019?

  • sam
    sam added an answer Please go launch below link   https://digitalcloud.training/aws-sysops-administrator-associate-free-practice-exam-questions/ August 3, 2023 at 9:55 am
  • newoc
    newoc added an answer Absolutely! You can confidently utilize authentic and up-to-date Dumps that… July 22, 2023 at 7:31 am
  • strapcart_online
    strapcart_online added an answer Impotence is commonly seen in men between the ages of… July 21, 2023 at 8:26 am
  • lasop
    lasop added an answer If you're looking for the ultimate resource to excel in… July 19, 2023 at 5:00 am
  • bruceparker
    bruceparker added an answer Buy Dapoxetine Online is a medication primarily used for the… July 17, 2023 at 7:34 am

Adv 120x600

Trending Categories

Health
2050Questions
, 4Followers
Programming
1296Questions
, 0Followers
Technology
218Questions
, 3Followers
General Knowledge
131Questions
, 0Followers
Business & Finance
86Questions
, 4Followers

Adv 120×600

Stats

  • Questions 4k
  • Answers 1k
  • Best Answers 105
  • Users 164

Users

Josephfauci

Josephfauci

  • 0 Questions
  • 0 Answers
Aluminium Die Casting Products Manufacturers

Aluminium Die Casting Products Manufacturers

  • 0 Questions
  • 0 Answers
irene1707

irene1707

  • 0 Questions
  • 0 Answers
Ductile Iron Casting Manufacturers

Ductile Iron Casting Manufacturers

  • 0 Questions
  • 0 Answers
Navratan

Navratan

  • 0 Questions
  • 0 Answers

Adv 120×600

Explore

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

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.