Python: Read and write to a CSV file

Source: Internet
Author: User

1. Python has a dedicated CSV package that can be imported directly.
Import CSV;
2. Open method for direct use of ordinary files
Csv_reader=open ("E:/python/csv_data/log.csv", ' R ') Data=[]for line in Csv_reader:    data.append (List (Line.strip () . Split (' | ')) For on data:    print (line)
3. Return to iteration type using Csv.reader & writer
Import sys;reload (SYS) sys.setdefaultencoding ("Utf-8"); Csv_reader=csv.reader (file (' E:\\python\csv_data\log.csv '), ' RB '))) for row in Csv_reader:    print (Row) writer=csv.writer (file (' E:\\python\csv_data\log_write.csv ', ' WB ') Writer.writerow ([' Name ', ' id ', ' comment ']) Lines=[range (3) for I in range (5)]for line in lines:    Writer.writerow (line )
4. Use CSV. Dictreader & Dictwriter, returns the Dict dictionary type.
Reader1=csv. Dictreader (File (' e:\\python\csv_data\women_write.csv ', ' RB '))

Reference

Python: Read and write to a 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.