How to read and write csv files in python

Source: Internet
Author: User
This article describes how to read and write csv files in python. how to read and write csv files in python

In data analysis, you often need to access data from csv files and write data into csv files. It is very convenient and easy to directly read the data in the csv file as the dict type and DataFrame. the following code takes 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 is set to None by default. if the csv file to be read has no header, you need to specify list_1 = [e for e in reader] # each row of data is saved as a dict to the csvfile in the linked list. 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 data entry needs to be processed separately and the data volume is large, it is recommended to process it one by one and then put it in.

List_1 = list () for e in reader: list_1.append (your_func (e) # your_func is the processing function of each data entry.
Write multiple dict data to a csv file

Code

# Data = [{'petal. length ': '1. 4', 'sepal. length ': '5. 1 ', 'petal. width ': '0. 2', 'sepal. width ': '3. 5', 'species ': 'setosa'}, {'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 '}] # header = ['petal. length ', 'sepal. length', 'petal. width', 'sepal. width', 'species '] print len (data) with open ('E:/dst.csv', 'WB ') as dstfile: # select wb as the write mode, otherwise, the empty row writer = csv. dictWriter (dstfile, fieldnames = header) writer. writeheader () # write the header writer. writerows (data) # batch write to dstfile. close ()

The above code writes the data into the csv file as a whole. if the data volume is large and you want to view the data written in real time, you can use the writerows function.

Read the csv file as DataFrame

Code

# Read the csv file DataFrameimport pandas as pddframe = pd. DataFrame. from_csv ('E:/iris.csv ')

It can also be slightly tortuous:

Import csvimport pandas as pdwith open ('E:/iris.csv ') as csvfile: reader = csv. dictReader (csvfile, fieldnames = None) # fieldnames is set to None by default. if the csv file to be read has no header, you need to specify list_1 = [e for e in reader] # each row of data is saved as a dict to the csvfile in the linked list. close () dfrme = pd. dataFrame. from_records (list_1)
Write a csv file into DataFrame
Dfrme. to_csv ('E:/dst.csv ', index = False) # do not specify the number of each line.

The above describes how to read and write csv files in python. For more information, see other related articles in the first PHP community!

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.