利用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
SQLite3中文编码 Python的实现
读取十万多条文本写入SQLite类型数据库,由于文本中存在中文字符,插入到数据库没错,取出时一直是UnicodeDecodeError,导致折腾了一天。最后的解决方
python使用正则表达式匹配字符串开头并打印示例
本文实例讲述了python使用正则表达式匹配字符串开头并打印的方法。分享给大家供大家参考,具体如下:importres="name=z1hangshanusername=fffurl=www.baidu.compasswor
Python中遇到的小问题及解决方法汇总
本文会把学习过程中遇到的一些小问题和解决办法放在这里,以便于大家能够更好地学习python。一、Python的异常处理因为想到自己不断尝试写小程序的
标签:字符串,中文,开头,本文,数据库