1:文件内容格式为json的数据如何解析
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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:出现频率统计
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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
1
2
|
import importlib import . reload (modulename) |
4:pylab中包含了哪些module
from pylab import *
等效于下面的导入语句:
1
2
3
4
|
from pylab import * from numpy import * from scipy import * import matplotlib |