Home  >  Article  >  Backend Development  >  How to read csv files in Python

How to read csv files in Python

(*-*)浩
(*-*)浩Original
2019-06-29 13:28:1417064browse

Comma-Separated Values ​​(CSV, sometimes also called character-separated values, because the delimiting character may not be a comma), its file stores tabular data (numbers and text) in plain text.

How to read csv files in Python

#Plain text means that the file is a sequence of characters and contains no data that must be interpreted like binary numbers. (Recommended learning: Python video tutorial)

CSV file consists of any number of records, separated by some kind of newline character; each record consists of fields, and the separators between fields is another character or string, most commonly a comma or tab. Usually, all records have exactly the same field sequence.

Reading of csv files:

Preliminary work: Create an excel file in the defined py file, And save it as a csv file, put in three lines of data, here is my name and age (you can write it yourself)

First we need to import the csv module in the python environment (I like to use it for testing newbies) pycharm)

#encoding:utf-8
import csv

Then we define the variable csv_file of a csv file, and then open the file through open. The opening mode uses 'r' (read: read mode). If you don't understand here, you can Baidu The access mode of the file under

is shown in the figure below:

How to read csv files in Python

The csv_file printed in the figure is just A model of an object (1 in the figure), we need to traverse and print this model. By printing, we can clearly see the data we printed

How to read csv files in Python

More For Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to read csv files in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn