python学习第一天
今天学习了一下python,主要是作了两个例子,总结了一下涉及到的概念。
先把写的例子放上来
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. import re,string is_all=[]#吧日志一条条分割 is_only={}#只有session和ip,用字典 is_fina={}#最后的结果 def is_find():#主要是用来得到is_all f=open('D:\\doc\\serachlog.txt','r') temp=f.readlines() for line in temp: is_all.append(re.split('\s\s+|\t|,',line.strip())) def is_list(ip_all): for i in range(len(ip_all)): is_only[ip_all[i][1]]=is_only[i][2] def is_select(ip_only): count=0 for values in ip_only.values(): for eachkey in ip_only.keys(): if values==ip_only[eachkey]: count=count+1 is_final[values]=count count=0 学习掌握了的知识点:
第一个。split函数的使用,用来分割一个字符串,is_all.append(re.split('\s\s+|\t|,',line.strip()))
这里面strip的作用默认的话是去掉一个字符串两端的空格,也可以用lstrip和rstrip分别来去除字符串的两边.
第二个:列表和字典。列表可以有二维的,其实和数组一样,另外就是字典,字典就是有key和values两部分,key是唯一的这个结果很重要,很多地方可以用到字典来保存数据.
第三个open一个文件不要忘记把他再close