Detailed python read and write CSV format file method

Source: Internet
Author: User

Python reads and writes CSV format files

In data analysis, it is often necessary to access data from CSV-formatted files and to write data to a CSV file. It is convenient and easy to read the data in the CSV file directly as Dict type and dataframe, and the following code takes the iris data as an example.

CSV file read as Dict

Code

#-*-Coding:utf-8-*-import csvwith open (' e:/iris.csv ') as Csvfile:reader = csv. Dictreader (CSVFile, Fieldnames=none)   # fieldnames default to None, if the read CSV file does not have a table header, you need to specify list_1 = [E for e in reader]  # Each row of data is stored as a dict in the linked list csvfile.close () print list_1[0]

Output

{' petal.length ': ' 1.4 ', ' sepal.length ': ' 5.1 ', ' petal.width ': ' 0.2 ', ' sepal.width ': ' 3.5 ', ' species ': ' Setosa '}

If each piece of data that is read needs to be processed separately and the amount of data is large, it is recommended to process it and then put it.

list_1 = List () for E in reader:  list_1.append (Your_func (e)) # Your_func the processing function for each data

Multiple types of dict data are written to the CSV file

Code

#   data = [{' Petal.length ': ' 1.4 ', ' sepal.length ': ' 5.1 ', ' petal.width ': ' 0.2 ', ' sepal.width ': ' 3.5 ', ' species ': ' SE Tosa '},{' petal.length ': ' 1.4 ', ' sepal.length ': ' 4.9 ', ' petal.width ': ' 0.2 ', ' sepal.width ': ' 3 ', ' species ': ' Setosa '},{' Petal.length ': ' 1.3 ', ' sepal.length ': ' 4.7 ', ' petal.width ': ' 0.2 ', ' sepal.width ': ' 3.2 ', ' species ': ' Setosa '},{' Petal.length ': ' 1.5 ', ' sepal.length ': ' 4.6 ', ' petal.width ': ' 0.2 ', ' sepal.width ': ' 3.1 ', ' species ': ' Setosa '}]#   Table header Header = [' Petal.length ', ' sepal.length ', ' petal.width ', ' sepal.width ', ' species ']print len (data) with open (' e:/ Dst.csv ', ' WB ') as Dstfile:   #写入方式选择wb, otherwise there is a blank line    writer = csv. Dictwriter (Dstfile, Fieldnames=header)    Writer.writeheader ()    #   Write header    writer.writerows (data)  # Bulk Write Dstfile.close ()

The code above writes the data to the CSV file as a whole, and uses the Writerows function if the amount of data is large and you want to see how much data is written in real time.

Read the CSV file as Dataframe

Code

# Read the CSV file for Dataframeimport pandas as Pddframe = PD. Dataframe.from_csv (' E:/iris.csv ')

You can also have a slightly zigzag point:

Import Csvimport Pandas as Pdwith open (' e:/iris.csv ') as CSVFile:    reader = csv. Dictreader (CSVFile, Fieldnames=none)   # fieldnames default to None, if the read CSV file does not have a table header, you need to specify    list_1 = [E for e in reader]  # Each row of data is stored as a dict in the linked list csvfile.close () Dfrme = PD. Dataframe.from_records (list_1)

Dataframe writing to a CSV file

Dfrme.to_csv (' E:/dst.csv ', index=false) # do not number each line
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.