利用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中使用pngquant压缩png图片的教程

说到png图片压缩,可能很多人知道TinyPNG这个网站。但PS插件要钱(虽然有破解的),Developer API要连到他服务器去,不提网络传输速度,Key也是有每月限制的。 &nbs...

python中Matplotlib实现绘制3D图的示例代码

Matplotlib 也可以绘制 3D 图像,与二维图像不同的是,绘制三维图像主要通过 mplot3d 模块实现。但是,使用 Matplotlib 绘制三维图像实际上是在二维画布上展示,...

在Django的session中使用User对象的方法

在Django的session中使用User对象的方法

通过session,我们可以在多次浏览器请求中保持数据, 接下来的部分就是用session来处理用户登录了。 当然,不能仅凭用户的一面之词,我们就相信,所以我们需要认证。 当然了,Dja...

django的auth认证,authenticate和装饰器功能详解

django的auth认证,authenticate和装饰器功能详解

在django中创建表,会自动创建一些django自带的表,先了解用户认证, 认证登录 先要引用 , from django.contrib import auth 有很多方法,...

python装饰器代替set get方法实例

对于变量的访问和设置,我们可以使用get、set方法,如下: class student: def __init__(self,name): self.__name =...