How to pass dictionary to functions?
<p>I want to pass dictionary to user defined functions and I need to do some calculation based on the dictionary values. It is not working for me with functions but works fine without using functions. I am not sure, what is wrong with code. Any help please? No error message.</p>

<p>Input:</p>

<blockquote>
  <p>"13-07-2016 12:55:46",user,192.168.10.100,192.168.10.20,CONNECT,200,"www.abc.com" <br>
  "13-07-2016 12:57:50",user,192.168.10.100,192.168.10.20,CONNECT,200,"www.abc.com" <br>
  "13-07-2016 13:00:43",user,192.168.10.100,192.168.10.20,CONNECT,200,"www.abc.com" <br>
  "13-07-2016 13:01:45",user,192.168.10.100,192.168.10.20,CONNECT,200,"www.abc.com" <br>
  "13-07-2016 13:02:57",user,192.168.10.100,192.168.10.20,CONNECT,200,"www.abc.com" <br>
  "13-07-2016 13:04:59",user,192.168.10.100,192.168.10.20,CONNECT,200,"www.abc.com" <br>
  "13-07-2016 13:06:51",user,192.168.10.100,192.168.10.20,CONNECT,200,"www.abc.com" <br>
  "13-07-2016 13:07:56",user,192.168.10.100,192.168.10.20,CONNECT,200,"www.abc.com"</p>
</blockquote>

<p>Code:</p>

<pre><code>file_name = sys.argv[1]
fo = open(file_name, "rb")


def setdict():
   dico,i={},0

   line = fo.readline()
   for 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):
   for k,v in dico.items():
      print k,v
</code></pre>

<p><strong>A:</strong> You should consider to call your functions at the end of the script:</p>

<pre><code>dico = setdict()
display(dico)
</code></pre>

<p>Without that, they are declared, but not used.</p>

<p><strong>B:</strong> You should also consider a better way to open your file:</p>

<pre><code>with open(file_name, "rb") as f:
  lines = f.readlines()
  for line in lines:
    # Do stuff with your line
</code></pre>

<p>This is the best way to open a file in python and to read it line by line.</p>

<p><strong>C:</strong> You are using:</p>

<pre><code>   line = fo.readline()
   # ^ That line is never use after, you will loose all it's datas
   for line in fo:
     #do stuff on line
</code></pre>

<p>I've add a comment to show you that you loose the data from the first line.</p>

<p><strong>D:</strong> You are using global variable (you use <code>fo</code> inside <code>setdict()</code> a better way will be to pass it by arguments:</p>

<pre><code>fo = open(file_name, "rb")

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

setdict(fo)
</code></pre>

<p>Finally, here is how you can rewrite your script :</p>

<pre><code>def setdict(filename):
   dico,i={},0
   with open(filename, 'r') as f:
   for 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):
   for k,v in dico.items():
      print k,v

file_name = sys.argv[1]
dico = setdict(filename)
display(dico)
</code></pre>

python dictionary