python导入txt文件并绘制成条形图
2015-10-13 11:11
633 查看
今天需要把从navicat导出的数据画成条形图,第0列为横坐标,第一列为纵坐标,导成txt格式后发现数据的格式是"1""163664"这样的,需要把双引号去掉放在数组中,用了split('"')分割符,"1""163664"分割后为
['', '1', '\t', '163664', '\n'],所以取第一列并转化为int型,先用open()打开文件,再遍历文件取需要的数据,代码如下:
A=[]
B = []
file = open('F:\python练习\LiveTVMediaChannel观看人数.txt')
for r in file:
channel = r.split('"')[1]
num_people = r.split('"')[3]
int_channel = string.atoi(channel)
int_num_people = string.atoi(num_people)
A.append(int_channel)
B.append(int_num_people)
p1 = plt.bar(A[],B[],width)
plt.show()
['', '1', '\t', '163664', '\n'],所以取第一列并转化为int型,先用open()打开文件,再遍历文件取需要的数据,代码如下:
A=[]
B = []
file = open('F:\python练习\LiveTVMediaChannel观看人数.txt')
for r in file:
channel = r.split('"')[1]
num_people = r.split('"')[3]
int_channel = string.atoi(channel)
int_num_people = string.atoi(num_people)
A.append(int_channel)
B.append(int_num_people)
p1 = plt.bar(A[],B[],width)
plt.show()
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法