Python3 using CSV module to read and write CSV files

Source: Internet
Author: User

To read a CSV file:

Import CSV
#打开文件, with the open can not be deliberately closed file, Python3 does not support files () Open, only with open ()
With open ("Xxx.csv", "R", encoding= "Utf-8") as CSVFile:
     #读取csv文件, the iteration type is returned
     Read = csv. Reader (CSVFile)
     For I in read:
          Print (i)
Save As CSV file:

Import CSV
With open ("Xxx.csv", "W", Newline= "") as Datacsv:
     #dialect为打开csv文件的方式, the default is excel,delimiter= "\ T" parameter refers to the delimiter at the time of writing
     Csvwriter = csv. writer (Datacsv,dialect = ("Excel"))
     #csv文件插入一行数据, put each item in the following list into a cell (you can insert multiple rows in a loop)
     Csvwriter.writerow (["A", "B", "C", "D"])

Description:CSV module also has Dictreader and dictwriter can be used to read and write, return is the type of dictionary, but these two methods I have no use, interested can see for themselves.

Python3 using CSV module to read and write CSV files

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.