摘要:在这篇文章中对于“在Python如何浏览CSV文件”中,咱们将学习如何读,写和解析的CSV文件的Python。
您晓得将表格数据存储到纯文本文件背地的机制是什么吗?答案是CSV(逗号分隔值)文件,该文件容许将数据转换为纯文本格式。在这篇文章中对于“在Python如何浏览CSV文件”中,咱们将学习如何读,写和解析的CSV文件的Python。
将具体探讨以下方面:
o 什么是CSV文件及其用处?
o 为什么应用CSV文件格式?
o Python CSV模块
§ CSV模块性能
o 在Python中执行写入,读取CSV文件的操作
让咱们开始吧。
什么是CSV文件及其用处?
CSV(逗号分隔值)是一种纯文本文件格局,用于存储表格数据(例如电子表格或数据库)。它实质上存储的表格数据包含数字和纯文本文本。大多数在线服务使用户能够自在地将网站中的数据导出为CSV文件格式。CSV文件通常会在Excel中关上,简直所有数据库都具备不同的特定工具以容许导入雷同的文件。
文件的每一行都称为记录。每个记录由用_逗号分隔_ 的字段组成,这些字段也称为“定界符”,这是默认定界符,其余记录包含pipe(|),分号(;)。上面给出的是一个一般CSV文件的构造,以逗号分隔,我正在应用一个泰坦尼克号CSV文件。
构造
`Passenger,Id,Survived,Pclass,Name,Sex.Age 1,0,3 Braund, Mr. Owen Harris ,male, 22 2,1,1 Cumings, Mrs. John Bradley (Florence Briggs Thayer), female,38 3,1,3 Heikkinen, Miss. Laina ,female, 26 4,1,1 Futrelle, Mrs. Jacques Heath (Lily May Peel),female,35`
持续说说应用CSV文件格式的起因。
为什么应用CSV文件格式?
CSV是纯文本文件,它使数据交换更容易,也更易于导入到电子表格或数据库存储中。例如:您可能心愿将某个统计分析的数据导出到CSV文件,而后将其导入电子表格以进行进一步剖析。总体而言,它使用户能够通过编程轻松地体验工作。任何反对文本文件或字符串操作的语言(例如Python)都能够
间接应用CSV文件。
继续前进,让咱们看看Python如何原生应用CSV。
Python CSV模块
Python应用的CSV软件包是规范库的一部分,因而您无需装置它。
import csv
当初,让我向您展现不同的CSV性能。
CSV模块性能
在CSV模块下,您能够找到以下性能:
Python中CSV文件的操作让咱们继续前进,从Python CSV文件上不同操作的编码角度来看。
加载CSV文件后,您能够执行多种操作。我将在Python中显示对CSV文件的读取和写入操作。
在Python中读取CSV文件:
import csv with open('Titanic.csv','r') as csv_file: #Opens the file in read mode csv_reader = csv.reader(csv_file) # Making use of reader method for reading the file for line in csv_reader: #Iterate through the loop to read line by line print(line)
输入:
在这里,从输入中能够看到,我曾经应用了Titanic CSV File。并且所有字段都用逗号分隔,文件被读入Python。
继续前进,让咱们看看如何写入CSV文件。
用Python写入CSV文件:
import csv with open('Titanic.csv', 'r') as csv_file: csv_reader = csv.reader(csv_file) with open('new_Titanic.csv', 'w') as new_file: # Open a new file named 'new_titanic.csv' under write mode csv_writer = csv.writer(new_file, delimiter=';') #making use of write method for line in csv_reader: # for each file in csv_reader csv_writer.writerow(line) #writing out to a new file from each line of the original file
out:
当初,这种应用读写器办法解决CSV文件的办法是最常见的办法之一。让咱们继续前进,看看如何应用python字典来做同样的事件。
读取CSV文件作为字典:
import csv with open('Titanic.csv','r') as csv_file: #Open the file in read mode csv_reader = csv.DictReader(csv_file) #use dictreader method to reade the file in dictionary for line in csv_reader: #Iterate through the loop to read line by line print(line)
输入:
从输入中能够看到,字段已被替换,它们当初充当字典的“键”。
让咱们看看如何将CSV文件作为字典写入。
作为字典写入CSV文件
import csv mydict = [{'Passenger':'1', 'Id':'0', 'Survived':'3'}, #key-value pairs as dictionary obj {'Passenger':'2', 'Id':'1', 'Survived':'1'}, {'Passenger':'3', 'Id':'1', 'Survived':'3'}] fields = ['Passenger', 'Id', 'Survived'] #field names filename = 'new_Titanic.csv' #name of csv file with open('new_Titanic.csv', 'w')as new_csv_file: #open a new file 'new_titanic,csv' under write mode writer = csv.DictWriter(new_csv_file, fieldnames=fields) writer.writeheader() #writing the headers(field names) writer.writerows(mydict) #writing data rows
输入:
让咱们看看如何在python中将CSV文件读取为熊猫。
以熊猫格局读取CSV文件:
import pandas #install pandas package result = pandas.read_csv('Titanic.csv') #read the csv file print(result) # print result
输入:
这使咱们到文章“如何在Python中读取CSV文件”的结尾。我心愿您对与CSV相干的所有概念,如何读写它,如何将CSV作为字典进行读写以及如何将CSV作为熊猫进行浏览都高深莫测。
确保尽可能多地练习并复原教训。
本文分享自华为云社区《如何在Python中读取CSV文件?》,原文作者:Yuchuan 。
点击关注,第一工夫理解华为云陈腐技术~