介绍
这篇文章给大家分享的是有关python中不同的CSV功能和使用示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
python主要应用领域有哪些
1,云计算,典型应用OpenStack.2,网前端开发,众多大型网站均为python开发。3。人工智能应用,基于大数据分析和深度学习而发展出来的人工智能本质上已经无法离开python.4,系统运维工程项目,自动化运维的标配就是python + Django/flask.5,金融理财分析,量化交易,金融分析。6,大数据分析。
<强>一、CSV模块功能强>
在CSV模块下,可以找到以下功能
<强>二,python中CSV文件操作强>
加载CSV文件后,您可以执行多种操作。将在python中显示对CSV文件的读取和写入操作
在python中读取CSV文件:
import csv , with 开放(& # 39;Titanic.csv& # 39; & # 39; " # 39;), as csv_file:, # Opens 从而file 拷贝read 模式 ,,,csv_reader =, csv.reader (csv_file), #, Making use of reader method for reading 从而文件 , ,,,for line csv_reader:拷贝,# Iterate through 从而loop 用read line by 线 ,,,,,,,印刷(线)
用Python写入CSV文件:
import csv with 开放(& # 39;Titanic.csv& # 39;,, & # 39; " # 39;), as csv_file: ,,,csv_reader =, csv.reader (csv_file), ,,,with 开放(& # 39;new_Titanic.csv& # 39;,, & # 39; w # 39;), as new_file:, #, Open a new file named & # 39; new_Titanic.csv& # 39; under write 模式 ,,,,,,,csv_writer =, csv.writer (new_file,分隔符=& # 39;;& # 39;),# making use of write 方法 , ,,,,,,,for line csv_reader:拷贝,#,for each file csv_reader拷贝 ,,,,,,,,,,,csv_writer.writerow(线),# writing out 用a new file 得到each line of 从而original 文件
读取CSV文件作为字典
import csv , with 开放(& # 39;Titanic.csv& # 39; & # 39; " # 39;), as csv_file:, # Open 从而file 拷贝read 模式 ,,,csv_reader =, csv.DictReader (csv_file), # use dictreader method 用reade 从而file 拷贝字典 , ,,,for line csv_reader:拷贝,# Iterate through 从而loop 用read line by 线 ,,,,,,,印刷(线)
作为字典写入CSV文件
import csv , mydict =,[{& # 39;乘客# 39;:& # 39;1 & # 39;,,& # 39;id # 39;: & # 39; 0 & # 39;,, & # 39;存活# 39;:& # 39;3 & # 39;},,# key-value pairs as dictionary obj ,,,,,,,,,{& # 39;乘客# 39;:& # 39;2 & # 39;,,& # 39;id # 39;: & # 39; 1 & # 39;,, & # 39;存活# 39;:& # 39;1 & # 39;}, ,,,,,,,,,{& # 39;乘客# 39;:& # 39;3 & # 39;,,& # 39;id # 39;: & # 39; 1 & # 39;,, & # 39;存活# 39;:& # 39;3 & # 39;}] , fields =,(& # 39;乘客# 39;,,& # 39;id # 39;,, & # 39;存活# 39;],# field 名字=,filename & # 39; new_Titanic.csv& # 39;, # name of csv 文件 ,with 开放(& # 39;new_Titanic.csv& # 39;,, & # 39; w # 39;) as new_csv_file:, # open a new file & # 39; new_titanic, csv # 39; under write 模式 ,,,writer =, csv.DictWriter (new_csv_file,字段名=字段), ,,,writer.writeheader (), # writing 从而头(field 名称) , ,,,writer.writerows (mydict), # writing data 行
感谢各位的阅读!关于“python中不同的CSV功能和使用示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!