利用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

相关文章

用Django实现一个可运行的区块链应用

用Django实现一个可运行的区块链应用

对数字货币的崛起感到新奇的我们,并且想知道其背后的技术——区块链是怎样实现的。   但是完全搞懂区块链并非易事,我喜欢在实践中学习,通过写代码来学习技术会掌握得更牢固。通过构建...

浅谈python的输入输出,注释,基本数据类型

1.输入与输出 python中输入与输出函数为:print、input help() 帮助的使用:help() help(print) print(value, ..., sep=...

Python实现京东秒杀功能代码

本文实例为大家分享了Python实现京东秒杀功能的具体代码,供大家参考,具体内容如下 #Python 3.5 #coding:utf-8 #import scrapy from s...

Python PyQt5 Pycharm 环境搭建及配置详解(图文教程)

Python PyQt5 Pycharm 环境搭建及配置详解(图文教程)

PyQt5相关安装 python 版本 python 3.6.3 1、安装PyQt5 执行命令: pip install pyqt5 2、安装PyQt5-tools 执行命令:pip...

Python调用ctypes使用C函数printf的方法

在Python程序中导入ctypes模块,载入动态链接库。动态链接库有三种:cdll以及windows下的windll和oledll,cdll载入导出函数使用标准的cdecl调用规范的库...