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

csv 读写 python_Python CSV读写相关推荐

  1. c++解析csv 存入数组_使用Apache Commons CSV在Java中读写CSV

    介绍 这是专门针对Java读写CSV的库的简短系列文章的第二篇,也是上一篇文章" Core Java读写CSV"的直接续篇. Apache Commons CSV 在Apache的 ...

  2. python csv读写方法_python中csv文件的若干读写方法小结

    如下所示: //用普通文本文件方式打开和操作 with open("'file.csv'") as cf: lines=cf.readlines() ...... //用普通文本方 ...

  3. python 数据分析-读写数据csv、xlsx文件

    1.读写csv文件可以使用基础python实现,或者使用csv模块.pandas模块实现. 基础python读写csv文件 读写单个CSV 以下为通过基础python读取CSV文件的代码,请注意,若字 ...

  4. python文件的模式,并进行csv,txt文件类型读写

    1. 文件的访问模式: 首先看一下文件的访问方式 访问模式 说明 r 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. w 打开一个文件只用于写入.如果该文件已存在则将其覆盖.如果该文 ...

  5. R语言文本文件读写(txt/csv/xlsx)

    这篇文章主要介绍了R语言文本文件读写(txt/csv/xlsx),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 本文主要介绍了R ...

  6. pyspark —— spark dataframe 从hdfs读写文件:按照指定文件格式读写文件(读写csv、json、text文件,读取hive表,读取MySQL表)、按照指定分隔符读写文件

    spark有3种数据结构--RDD.DataFrame.DataSet.这里展示的文件读写方式,都是针对dataFrame数据结构的,也就是文件读进来之后,是一个spark dataFrame. 0. ...

  7. python3 csv读写_python3 csv

    一.python3 csv 的中文乱码解决方案 将文件保存为 csv 格式的话,用记事本打开是没有问题的,但用excel 打开就会乱码,在网上找了些解决方法都是适用python2 这里提供下一个解决方 ...

  8. R语言-文本文件读写 txt / csv / xlsx

    read.table(file,sep,hesder) #file 文件路径 #sep 分隔符 #header 第一行是不是列名(如果第一行是列名导入的时候填TRUE:默认值是FALSE,即把第一行算 ...

  9. 【Python 文件读写】— csv文件

    目录 一.CSV文件简介 二.使用csv库读文件 1.两种方式 2.csv.reader()示例 3.csv.DictReader()示例 三.使用csv库写文件 1.两种方式 2.csv. writ ...

最新文章

  1. 通俗易懂理解~图机器学习导论
  2. spring boot + vue + element-ui全栈开发入门——前后端整合开发
  3. miui 8.5 android,小米MIUI 8.5稳定版更新来了:直达服务功能秒开应用
  4. 深入浅出SNMP及其应用实例分析
  5. 编写一个算法frequency,统计在一个输入字符串中各个不同字符出现的频度。用适当的测试数据来验证这个算法。
  6. 人工智能、物联网和大数据如何拯救蜜蜂
  7. 数据库和ORMS:使用SQLAlchemy与数据库通信
  8. 异步编程到底在说啥?
  9. Swagger介绍及使用
  10. 项目Alpha冲刺Day3
  11. 笔记:HPlus模板 单选框
  12. 内蒙古自治区呼伦贝尔市谷歌高清卫星地图下载
  13. edem合成运动教程
  14. Windows下使用SSH命令登录Linux服务器
  15. 【Python入门】Turtle海龟库:利用海龟画笔绘制正方形和圆形
  16. (copy即运行)嗖嗖移动大厅详讲!!!一看便知
  17. JavaWeb之servlet(1)
  18. 记录ESP32 出现未定义vTaskGetRunTimeStats的奔溃时刻!
  19. MSDB数据库置疑状态的解决方法
  20. Python编曲实践(二):和弦的实现和进行

热门文章

  1. 《面向对象程序设计》第六次作业(图形化界面)
  2. jquery mobile将页面内容当成弹框进行显示
  3. XCode6 ,iOS之PCH文件配置
  4. UVALive 4212 Candy
  5. [转载] Python repr() 函数
  6. 简单解决 WIN10更新后 远程桌面提示 CredSSP加密Oracle修正的问题
  7. 匿名函数lambda
  8. 浅谈数据结构之顺序队列(五)
  9. JavaScript错误和异常
  10. hdu 4059 The Boss on Mars 容斥