利用python实现数据分析

yipeiwu_com6年前Python基础

1:文件内容格式为json的数据如何解析

import json,os,sys
current_dir=os.path.abspath(".")

filename=[file for file in os.listdir(current_dir) if ".txt" in file]#得到当前目录中,后缀为.txt的数据文件
fn=filename[0] if len(filename)==1 else "" #从list中取出第一个文件名

if fn: # means we got a valid filename
  fd=open(fn)
  content=[json.loads(line) for line in fd]
  
else:
  print("no txt file in current directory")
  sys.exit(1)
for linedict in content:
  for key,value in linedict.items():
    print(key,value)
  print("\n")

2:出现频率统计

import random
from collections import Counter
fruits=[random.choice(["apple","cherry","orange","pear","watermelon","banana"]) for i in range(20)]
print(fruits) #查看所有水果出现的次数

cover_fruits=Counter(fruits)
for fruit,times in cover_fruits.most_common(3):
  print(fruit,times)

########运行结果如下:apple在fruits里出了5次
apple 5  
banana 4
pear 4

3:重新加载module的方法py3

import importlib
import.reload(modulename)

4:pylab中包含了哪些module

   from pylab import *

等效于下面的导入语句:

  from pylab import *
  from numpy import *
  from scipy import *
  import matplotlib

相关文章

Python生成随机数组的方法小结

Python生成随机数组的方法小结

本文实例讲述了Python生成随机数组的方法。分享给大家供大家参考,具体如下: 研究排序问题的时候常常需要生成随机数组来验证自己排序算法的正确性和性能,今天把Python生成随机数组的方...

python实现异常信息堆栈输出到日志文件

将try except中捕获到的异常信息输出到日志文件中,方便查找错误原因,tranceback模块提供了把详细出错堆栈信息格式化成字符串返回函数format_exc(). 具体代码如下...

python实现下载文件的三种方法

Python开发中时长遇到要下载文件的情况,最常用的方法就是通过Http利用urllib或者urllib2模块。 当然你也可以利用ftplib从ftp站点下载文件。此外Python还提...

使用Python的Turtle绘制哆啦A梦实例

使用Python的Turtle绘制哆啦A梦实例

这是我几年前为了练习python的turtle库而画的,今天翻出了代码,分享给大家。 这是我初学python时画的,当时还没有面向对象的概念,也没有采取类方法之类,纯原始手工,供大家参考...

在Python中使用turtle绘制多个同心圆示例

在Python中使用turtle绘制多个同心圆示例

我就废话不多说了,直接上代码吧! import turtle t = turtle.Pen() my_colors = ("red","green","yellow","black"...