csv 读写 python_Python CSV读写

csv 读写 python

In this tutorial we will learn about the python csv module that already exist with the python. In our previous tutorial, we have seen python mysql example.

在本教程中,我们将了解python已经存在的python csv模块。 在之前的教程中,我们看到了python mysql示例

Python CSV (Python CSV)

CSV stands for comma separated values. In this tutorial we will see how to read and write CSV files in python. Python provides a module named csv, using this we can do several operations on the csv files.

CSV代表逗号分隔的值。 在本教程中,我们将看到如何在python中读取和写入CSV文件。 Python提供了一个名为csv的模块,使用它我们可以对csv文件进行一些操作。

Python CSV文件 (Python CSV file)

As I said before CSV is a file format. Python CSV module will help to read and write in the csv file.

如我之前所说,CSV是一种文件格式。 Python CSV模块将有助于读取和写入csv文件。

The following is an example of csv file. The file name is employeeInfo.csv that is taken from an excel sheet containing the information of employee name, department and email address.

以下是csv文件的示例。 文件名是employeeInfo.csv ,该文件名取自包含工作人员姓名,部门和电子邮件地址信息的excel工作表。

employeeInfo.csv

employeeInfo.csv

Employee Name,Department,Email Address
Rizvi,MEC,rizvy@yahoo.com
Mamun,EECE,mamun@hotmail.com
Shamsujjaman,CSC,shams@gmail.com
Anika,ECE,anika@gmail.com
Zinia,CSE,xinia@yahoo.com
Nazrul,AE,nazrul@hotmail.com

We have to keep this csv file in the same directory from where we want to access this file using python.

我们必须将此csv文件保存在我们要使用python访问该文件的目录中。

Python读取CSV (Python Read CSV)

We can read the contents of csv file as followings with the help of csv.reader() method.

我们可以通过csv.reader()方法读取csv文件的内容,如下所示。

#importing csv
import csv

#openning the csv file which is in the same location of this python file
File = open('employeeInfo.csv')

#reading the File with the help of csv.reader()
Reader = csv.reader(File)

#storing the values contained in the Reader into Data
Data = list(Reader)

#printing the each line of the Data in the console
for data in Data:
   print(data)
File.close()

Below image shows the output produced by above python csv read example program.

下图显示了以上python csv read示例程序产生的输出。

So we have read our csv file. What if we want to get the specific columns like only name and email address. Then we have to do as following:

因此,我们已经阅读了csv文件。 如果我们想获取特定的列,例如仅姓名和电子邮件地址,该怎么办? 然后,我们必须执行以下操作:

#importing csv
import csv

#openning the csv file which is in the same location of this python file
File = open('employeeInfo.csv')

#reading the File with the help of csv.reader()
Reader = csv.reader(File)

#storing the values contained in the Reader into Data
Data = list(Reader)

#printing the 0th and 2nd column of each line of the Data in the console
for data in Data:
   print(data[0],' | ', data[2])
File.close()

It will output only the name and email address of the employee.

它只会输出员工的姓名和电子邮件地址。

Python CSV编写器 (Python CSV Writer)

You can also write in csv file using python csv module. To write in the file you can open the file in write mode or you may open the file in append mode.

您也可以使用python csv模块在csv文件中编写。 要写入文件,可以在写入模式下打开文件,也可以在追加模式下打开文件。

Then you have to use python csv.writer() to write in the csv file. The following is a example which writes in a csv file named output.csv.

然后,您必须使用python csv.writer()来写入csv文件。 以下是写入名为output.csv的csv文件的output.csv

#importing csv
import csv
# opening a file in write mode and newline = ''
# otherwise output.csv will contain two newline after writing each line.
File = open('output.csv', 'w', newline ='')

# setting the writer to the File
Writer = csv.writer(File)

# writing some values
Writer.writerow(['Yantai' , 'Resturant'])
Writer.writerow(['Convension Hall' , 'Community Center'])
Writer.writerow(['Lalbag Kella' , 'Historical Architecture'])

# closing the file
File.close()

Now you will see a file named output.csv in the same directory. Open that file, and you will find the values that you have wrote in it.

现在,您将在同一目录中看到一个名为output.csv的文件。 打开该文件,您将找到在其中写入的值。

To know more about python csv I should recommend you to visit the official website.

要了解有关python csv的更多信息,我建议您访问官方网站

翻译自: https://www.journaldev.com/15543/python-csv-read-write

csv 读写 python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值