Python read/write CSV file

Source: Internet
Author: User

Python comes with a CSV module, and if you want to know more about it, you can refer to the official documentation

First, read operation

I do not know why, if you open the file without using the ' B ' mode, there will be interlaced, so in Windows want to normal operation of the CSV file, plus b mode. Delimiter to specify the delimiter between the various fields of reader.

def readdata (): With    open ('csvfile.csv','rb'  as fobj:         = Csv.reader (fobj,delimiter='-');         = Next (csvfilereader);        Print header        for in  csvfilereader:            print row;

Second, write operation

Header = ["name","Sex","Age"]; Datas= [("Li Fei","male", the),             ("Zhang Shan","male", -)]; With open ('Csvfile.csv','WB') asFobj:csvfilewriter= Csv.writer (fobj,delimiter='-');        Csvfilewriter.writerow (header); Csvfilewriter.writerows (datas);

These are simple CSV file read and write operations, for more detailed documentation, please refer to the official documentation

Python read/write CSV file

Related Article

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.