How to read and write CSV format files in Python

Source: Internet
Author: User

It is common to read and write CSV files at work. Record, to facilitate their own future inquiries and share with you:

Write CSV file method one:

Import CSV #导入CSV

With open ("D:\egg.csv", "WB") as CSVFile #新建一个叫egg. csv "file on D-disk.

A=csv.writer (csvfile) #以CSV的格式 write data to file CSVFile.

A.writerow (["Name", "Age", "tel"]) #写入的数据.

Print CSVFile #打印

Csvfile.close #关闭文件

Write CSV file Method two:

Same principle as method one.

 import csv 
csvfile= File ( "D:\egg2.csv", A=csv.writer ( csvfile)
A.writerow ([ "name", " tel "])
print csvfile
csvfile.close ()

Read csv file
Import CSV
# csvfile=file ("D:\egg2.csv", "RB")
# Reader=csv.reader (CSVFile)
# for-line in reader: #用FOR A walk through, you can read the contents of the file.
# Print Line

is not very simple, you can also, hands-on practice up.

How to read and write CSV format files in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.