Note that there are some explanatory texts on larger screens.

plurals
  1. POCSV calulation script not saving the correct amount of columns
    primarykey
    data
    text
    <p>@Stackoverflow</p> <p>I have a script that adds three columns to the end of my csv based on matching 3 criteria. </p> <p>I understand the problem, is that the code is re-reading line from rdr and that wrt is the modified line, after the initial loop I tried to iterate through wrt, but the code was unable to do that.</p> <h3>My code:</h3> <pre><code>import csv import datetime import copy from collections import defaultdict with open(r"FullMergedData.csv") as i, open(r"FullMergedDataWSPSR.csv", "wb") as o: rdr = csv.reader(i, delimiter ="|") wrt = csv.writer(o, delimiter ="|") data, currdate = defaultdict(lambda:[0, 0, 0, 0]), None calclist = [6, 7, 19, 23, 25, 26, 35, 62, 64] for calc in calclist: for counter2, line in enumerate(rdr): if counter2 != 0: date, name = datetime.datetime.strptime(line[50], '%d/%m/%Y'), line[calc] if date != currdate or not currdate: for v in data.itervalues(): v[:2] = v[2:] currdate = date top,bottom = data[name][0:2] try: quotient = float(top)/bottom except ZeroDivisionError: quotient = 0 wrt.writerow(line + data[name][:2]+ [quotient]) data[name][3] += 1 if line[38] == "1": data[name][2] += 1 </code></pre> <p>If anyone can help with getting this code to work I would be very appreciative. Many thanks AEA</p> <h3>Code that produces traceback</h3> <pre><code>import csv import datetime import copy from collections import defaultdict with open(r"FullMergedData.csv") as i, open(r"FullMergedDataWSPSR.csv", "wb") as o: rdr = csv.reader(i, delimiter ="|") wrt = csv.writer(o, delimiter ="|") data, currdate = defaultdict(lambda:[0, 0, 0, 0]), None calclist = [6, 7, 19, 23, 25, 26, 35, 62, 64] for counter, calc in enumerate(calclist): if counter == 0: for counter2, line in enumerate(rdr): if counter2 != 0: date, name = datetime.datetime.strptime(line[50], '%d/%m/%Y'), line[calc] if date != currdate or not currdate: for v in data.itervalues(): v[:2] = v[2:] currdate = date top,bottom = data[name][0:2] try: quotient = float(top)/bottom except ZeroDivisionError: quotient = 0 wrt.writerow(line + data[name][:2]+ [quotient]) data[name][3] += 1 if line[38] == "1": data[name][2] += 1 else: for line in wrt: date, name = datetime.datetime.strptime(line[50], '%d/%m/%Y'), line[calc] if date != currdate or not currdate: for v in data.itervalues(): v[:2] = v[2:] currdate = date top,bottom = data[name][0:2] try: quotient = float(top)/bottom except ZeroDivisionError: quotient = 0 wrt.writerow(line + data[name][:2]+ [quotient]) data[name][3] += 1 if line[38] == "1": data[name][2] += 1 </code></pre> <h3>Traceback</h3> <pre><code>Traceback (most recent call last): File "C:\mergecalculator.py", line 35, in &lt;module&gt; for line in wrt: TypeError: '_csv.writer' object is not iterable </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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