如何在Python中解析CSV⽂件
CSV files are used a lot in storing tabular data into a file. We can easily export data from database tables or excel files to CSV files. It’s also easy to read by humans as well as in the program. In this tutorial, we will learn how to parse CSV files in Python.
CSV⽂件⽤于将表格数据存储到⽂件中。 我们可以轻松地将数据从数据库表或Excel⽂件导出到CSV⽂件。 它也很容易被⼈类以及程序读取。 在本教程中,我们将学习如何在Python中解析CSV⽂件。
什么是解析? (What is Parsing?)
Parsing a file means reading the data from a file. The file may contain textual data so-called text files, or they may be a spreadsheet.
解析⽂件意味着从⽂件中读取数据。 该⽂件可能包含称为⽂本⽂件的⽂本数据,或者它们可以是电⼦表格。
什么是CSV⽂件? (What is a CSV file?)
CSV stands for Comma Separated Files, i.e. data is separated using comma from each other. CSV file
s are created by the program that handles a large number of data. Data from CSV files can be easily exported in the form of spreadsheet and database as well as imported to be used by other programs.
CSV代表逗号分隔⽂件,即,数据之间使⽤逗号分隔。 CSV⽂件是由处理⼤量数据的程序创建的。 CSV⽂件中的数据可以以电⼦表格和数据库的形式轻松导出,也可以导⼊以供其他程序使⽤。
Let’s see how to parse a CSV file. Parsing CSV files in Python is quite easy. Python has an inbuilt CSV library which provides the functionality of both readings and writing the data from and to CSV files. There are a variety of formats available for CSV files in the library which makes data processing user-friendly.
让我们看看如何解析CSV⽂件。 在Python中解析CSV⽂件⾮常容易。 Python有⼀个内置的CSV库,该库提供了从CSV⽂件读取数据和将数据写⼊CSV⽂件的功能。 库中CSV⽂件有多种格式可供使⽤,这使数据处理变得⽤户友好。
⽤Python解析CSV⽂件 (Parsing a CSV file in Python)
Reading CSV files using the inbuilt Python CSV module.
使⽤内置的Python CSV模块读取CSV⽂件。
import csv
with open('university_records.csv', 'r') as csv_file:
reader = ader(csv_file)
for row in reader:
print(row)
Output:
输出:
Python Parse CSV File
Python解析CSV⽂件
⽤Python编写CSV⽂件 (Writing a CSV file in Python)
For writing a file, we have to open it in write mode or append mode. Here, we will append the data to the existing CSV file.要写⼊⽂件,我们必须以写⼊模式或附加模式打开它。 在这⾥,我们会将数据附加到现有的CSV⽂件中。
import csv
row = ['David', 'MCE', '3', '7.8']
row1 = ['Lisa', 'PIE', '3', '9.1']
row2 = ['Raymond', 'ECE', '2', '8.5']
with open('university_records.csv', 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(row)
writer.writerow(row1)
writer.writerow(row2)
Python Append To CSV File
Python附加到CSV⽂件
使⽤Pandas库解析CSV⽂件 (Parse CSV Files using Pandas library)
There is one more way to work with CSV files, which is the most popular and more professional, and that is using the library.
还有另⼀种使⽤CSV⽂件的⽅法,它是最受欢迎和更专业的,并且使⽤了库。
Pandas is a Python data analysis library. It offers different structures, tools, and operations for working and manipulating given data which is mostly two dimensional or one-dimensional tables.
Pandas是⼀个Python数据分析库。 它提供了不同的结构,⼯具和操作来处理和处理给定的数据,这些数据主要是⼆维表或⼀维表。
熊猫图书馆的⽤途和特点 (Uses and Features of pandas Library)
Data sets pivoting and reshaping.
数据集透视和重塑。
Data manipulation with indexing using DataFrame objects.
使⽤DataFrame对象建⽴索引的数据操作。
python怎么读csv数据Data filtration.
数据过滤。
Merge and join operation on data sets.
对数据集的合并和联接操作。
Slicing, indexing, and subset of massive datasets.
⼤规模数据集的切⽚,索引和⼦集。
Missing data handling and data alignment.
缺少数据处理和数据对齐。
Row/Column insertion and deletion.
⾏/列的插⼊和删除。
One-Dimensional different file formats.
⼀维不同的⽂件格式。
Reading and writing tools for data in various file formats.
各种⽂件格式数据的读写⼯具。
To work with the CSV file, you need to install pandas. Installing pandas is quite simple, follow the instructions below to install it using PIP.
要使⽤CSV⽂件,您需要安装熊猫。 安装熊猫⾮常简单,请按照以下说明使⽤PIP进⾏安装。
$ pip install pandas
Python Install Pandas
Python安装熊猫
Python Install Pandas Cmd
Python安装Pandas Cmd
Once the installation is complete, you are good to go.
安装完成后,⼀切就好了。
使⽤Pandas Module读取CSV⽂件 (Reading a CSV file using Pandas Module)
You need to know the path where your data file is in your filesystem and what is your current working directory before you can use pandas to import your CSV file data.
您需要先了解数据⽂件在⽂件系统中的路径以及当前的⼯作⽬录,然后才能使⽤熊猫导⼊CSV⽂件数据。
I suggest keeping your code and the data file in the same directory or folder so that you will not need to specify the path which will save you time and space.
我建议将代码和数据⽂件保存在相同的⽬录或⽂件夹中,这样您就⽆需指定路径来节省时间和空间。
import pandas
result = ad_csv('ign.csv')
print(result)
Output
输出量
Read CSV File using pandas module
使⽤pandas模块读取CSV⽂件
使⽤Pandas模块编写CSV⽂件 (Writing a CSV file using Pandas Module)
Writing CSV files using pandas is as simple as reading. The only new term used is DataFrame.
使⽤熊猫编写CSV⽂件就像阅读⼀样简单。 使⽤的唯⼀新术语是DataFrame 。
Pandas DataFrame is a two-dimensional, heterogeneous tabular data structure (data is arranged in a tabular fashion in rows and columns.
Pandas DataFrame是⼆维的异构表格数据结构(数据以表格的形式排列在⾏和列中。
Pandas DataFrame consists of three main components – data, columns, and rows – with a labeled x-axis and y-axis (rows and columns).
Pandas DataFrame由三个主要组件组成-数据,列和⾏-带有标记的x轴和y轴(⾏和列)。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论