Note that there are some explanatory texts on larger screens.

plurals
  1. POPython (beginner): CSV column writing, unexpected output for time-autocorrelation function
    text
    copied!<p>Trying to write a fourth column to a set of data which looks like this </p> <pre><code> 8000.5 16745 0.1257 8001.0 16745 0.1242 8001.5 16745 0.1565 8002.0 16745 0.1595 </code></pre> <p>Which has the number of times the second number (i.e. 16745) has been counted in that particular file (it does change, the list has a couple thousand entries). i.e. if this were the whole file</p> <pre><code> 8000.5 16745 0.1257 4 8001.0 16745 0.1242 4 8001.5 16745 0.1565 4 8002.0 16745 0.1595 4 </code></pre> <p>The problem with my code seems to be in the writing stage, the dictionary works, and csv.reader is reading the file fine if I print it but when it comes to appending the only dictionary key it picks up (field[1]) seems to be one where 16745 is -1 and the count of this is printed in the fourth column for all rows. I can't understand why it is cross referencing with the dictionary only for this value and not on a per row basis.</p> <p>i.e. I get </p> <pre><code> 8000.5 16745 0.1257 [count of -1 in column 2] 8001.0 16745 0.1242 [count of -1 in column 2] 8001.5 16745 0.1565 [count of -1 in column 2] 8002.0 16745 0.1595 [count of -1 in column 2] </code></pre> <p>Any help would be greatly appreciated! </p> <pre><code>import numpy import string import csv import sys import os time = [] water = [] itemcount ={} global filename filename = sys.argv[1] f1 = open(sys.argv[1], 'rt') for line in f1: fields = line.split() time.append(fields[0]) water.append(fields[1]) f1.close() for x in water: a = water.count(x) itemcount[x] = a writerfp = open('watout.csv', 'w') writer = csv.writer(writerfp) for row in csv.reader(open(filename, 'r')): fields = line.split() row.append(itemcount[fields[1]]) writer.writerow(row) writerfp.close() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload