Nicholas
Nicholas

Reputation: 3737

Python - CSV Writing - cutting off final rows

I am writing a function to a CSV file (which is working), however it is cutting off halfway on one of the final rows. I know it is probably something to do with the closing of the file, but I thought I did it correctly.

Any suggestions where it may be going wrong?

from itertools import combinations as cb 
import csv
import numpy as np


with open("usableReviewScores.csv") as f:
    reader=csv.reader(f)
    next(reader, None)  # skip header
    data=[filter(None,i) for i in reader]

writer = csv.writer(open("alexData1.csv", 'wb'))

def avgg(x):
    ll=[float(i) for i in x[1:]] #take review no and convert to float
    n=len(ll)
    avg_list=[x[0]]  #start result list with ref no.
    final_list=[]
    a = 0
    b = []
    c = []
    d = []

    global min_val
    global max_val
    min_val = 0
    max_val = 0

    for i in range(4,5):
        for j in cb(ll,i):
            # print(j)
            c = i
            avg_list.append(sum(j)/i)
            final_list.append(sum(j)/i)
            a = sum(final_list)/len(final_list)
            min_val = min(final_list)
            max_val = max(final_list)
            d = np.std(final_list)

    return (avg_list, "avg", a, "min", min_val, "max", max_val,
        "Num of reviews", c, "std", d, "Total Reviews", n)

for x in data:
    print(avgg(x))

for x in data:
    writer.writerow(avgg(x))

Upvotes: 1

Views: 608

Answers (1)

SiHa
SiHa

Reputation: 8411

You say that it's probably to do with the closing of the file. Well you don't actually close your output file at all. So I'm guessing that this is a symptom of file-system caching and the cache not being properly flushed because the file isn't closed

You should use with open(filename) as handle: for the writing as well as for your input:

with open("alexData1.csv", 'wb') as outfile:
    writer = csv.writer(outfile)
    for x in data:
        writer.writerow(avgg(x))

Upvotes: 2

Related Questions