利用Python处理CSV 文件

CSV 文件:将数据作为一系列以逗号分隔的值写入文件,通俗的讲就是两个逗号的信息之间看作一个数据。


#csv模块包含在Python标准库中,可用于分析CSV文件中的数据行
import csv
#利用matplotlib绘图
from matplotlib import pyplot as plt

filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:
	#创建与文件相关联的阅读器对象
	reader = csv.reader(f)
	#next()函数返回文件的下一行,这里只调用了一次返回的是文件的第一行,其中包括文件头
	
	header_row = next(reader)
	
	highs = []
	
	for row in reader:
		high = int(row[1])
		highs.append(high)
	print(highs)
	
	fig  = plt.figure(dpi=128, figsize=(10, 6))
	plt.plot(highs, c='red')
	
	plt.title("Daily high temperatures, july 2014", fontsize=24)
	plt.xlabel('', fontsize=16)
	plt.ylabel("Temperature (F)", fontsize=16)
	plt.tick_params(axis='both', which='major', labelsize=16)
	
	plt.show()

程序中的文件数据使用的《Python从入门到实践》中的数据,可从https://www.nostarch.com/pythoncrashcourse/下载,第十六章数据。

 

输出结果

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值