Get grouped Dictionary list from a file that has a time and errors then plot the time differences in python
<p>I have this file as below:</p>

<pre><code>Date;Time;Task;Error_Line;Error_Message
03-13-15;08:2123:10;C:LOGINMAN;01073;Web Login Successful from IP Address xxx.xxx.x.xx
03-13-15;05:23:1235;B:VDOM;0906123;Port 123 Device 1012300 Remote 1 1012301 Link Up RP2009
03-13-15;05:23:123123;A:VCOM;0906123;Port 123 Device 1012300 Remote 1 1012301 Link Up RP2009
03-13-15;05:23:123123;B:VDOM;1312325;Port 123 Device 1012300 Remote 1 1012301 Receive Time Error: 2123666 23270 1396 69
03-13-15;05:23:1233;B:VDOM;13372;Port 123 Device 1012300 Remote 1 1012301 Send Time Error: 123123123 1888 1123123123 69
03-13-15;05:23:1233;A:VCOM;1312325;Port 123 Device 1012300 Remote 1 1012301 Receive Time Error: 2123666 23270 1396 69
03-13-15;05:23:1233;A:VCOM;13372;Port 123 Device 1012300 Remote 1 1012301 Send Time Error: 123123123 1888 1123123123 69
03-13-15;05:21:56;B:VDOM;07270;Port 123 Device 1012300 Remote 1 1012301 AT Timer Expired
03-13-15;05:21:56;A:VCOM;07270;Port 123 Device 1012300 Remote 1 1012301 AT Timer Expired
</code></pre>

<p>The desired output should be like that:</p>

<pre><code>D = {'Error_line1': [Time1,Time2,...],'Error_Line2' = [Time1,Time2,..],...}
</code></pre>

<p>I was looking for plotting the differences between or time based on Error_Line. Error_Line in my file occurs different time . I want group times according to Error_Line. I have have no idea if that works for plotting time.</p>

<p>I won't touch the plotting because there are multiple ways of displaying the data and I don't know what style you're looking for. Do you want to have separate graphs for each Error_Line? Each Error_Line's datapoints represented on one graph? Some other way of comparing times and errors (e.g. mean of each Error_Line's times plotted against each other, variance, yadda yadda)?</p>

<p>Getting that info into a dict, however, will involve getting each line, splitting it with the semicolon as the delimiter, and picking the pieces out that you want. Personally I'd do this as such:</p>

<pre><code>from collections import defaultdict
ourdata = defaultdict(list)
with open('stackoverflow.txt') as ourfile:
    for row in ourfile:
        datatoadd = row.split(';')[1:4:2]
        ourdata[datatoadd[1]].append(datatoadd[0])
</code></pre>

<p>As far as those timestamps go they're currently strings. You'll also need to convert them (within the append statement would do it all at once) to the data type you need (e.g. numpy's <a href="http://docs.scipy.org/doc/numpy/reference/arrays.datetime.html" rel="nofollow">datetimes</a> which allow for arithmetic).</p>

<p>For more information on what's going on here, see: <a href="https://docs.python.org/2/library/collections.html#collections.defaultdict" rel="nofollow">defaultdict</a>, <a href="http://stackoverflow.com/a/1369553/6758601">with</a>, <a href="https://docs.python.org/2/library/stdtypes.html#str.split" rel="nofollow">str.split()</a>, <a href="http://stackoverflow.com/a/509295/6758601">extended slice notation</a></p>

python numpy matplotlib plot