python - How to pass dictionary to functions? -


i want pass dictionary user defined functions , need calculation based on dictionary values. not working me functions works fine without using functions. not sure, wrong code. please? no error message.

input:

"13-07-2016 12:55:46",user,192.168.10.100,192.168.10.20,connect,200,"www.abc.com"
"13-07-2016 12:57:50",user,192.168.10.100,192.168.10.20,connect,200,"www.abc.com"
"13-07-2016 13:00:43",user,192.168.10.100,192.168.10.20,connect,200,"www.abc.com"
"13-07-2016 13:01:45",user,192.168.10.100,192.168.10.20,connect,200,"www.abc.com"
"13-07-2016 13:02:57",user,192.168.10.100,192.168.10.20,connect,200,"www.abc.com"
"13-07-2016 13:04:59",user,192.168.10.100,192.168.10.20,connect,200,"www.abc.com"
"13-07-2016 13:06:51",user,192.168.10.100,192.168.10.20,connect,200,"www.abc.com"
"13-07-2016 13:07:56",user,192.168.10.100,192.168.10.20,connect,200,"www.abc.com"

code:

file_name = sys.argv[1] fo = open(file_name, "rb")   def setdict():    dico,i={},0     line = fo.readline()    line in fo:      date, user, proxy_ip, client_ip, access_method, con, sites = line.split(",")      sites = sites.rstrip('\n')      dico[i]= date, user, proxy_ip, client_ip, access_method, con, sites    return dico  def display(dico):    k,v in dico.items():       print k,v 

a: should consider call functions @ end of script:

dico = setdict() display(dico) 

without that, declared, not used.

b: should consider better way open file:

with open(file_name, "rb") f:   lines = f.readlines()   line in lines:     # stuff line 

this best way open file in python , read line line.

c: using:

   line = fo.readline()    # ^ line never use after, loose it's datas    line in fo:      #do stuff on line 

i've add comment show you loose data first line.

d: using global variable (you use fo inside setdict() better way pass arguments:

fo = open(file_name, "rb")  def setdict(fo):    dico,i={},0    line = fo.readline()    ...  setdict(fo) 

finally, here how can rewrite script :

def setdict(filename):    dico,i={},0    open(filename, 'r') f:    line in f.readlines():      date, user, proxy_ip, client_ip, access_method, con, sites = line.split(",")      sites = sites.rstrip('\n')      dico[i]= date, user, proxy_ip, client_ip, access_method, con, sites    return dico  def display(dico):    k,v in dico.items():       print k,v  file_name = sys.argv[1] dico = setdict(filename) display(dico) 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -