Monday, 15 March 2010

python - appending array breaks program -



python - appending array breaks program -

i writing programme analyze of our invoice data. basically,i need take array containing each individual invoice sent out on past year & break downwards twelve arrays contains invoices month using dateseperate() function, monthly_transactions[0] returns januaries transactions, monthly_transactions[1] returns februaries & forth.

i've managed working dateseperate returns monthly_transactions[0] jan transactions. however, 1 time of jan info entered, effort append monthly_transactions array using line 44. however, causes programme break & become unrepsonsive. code still executes & doesnt homecoming error, python becomes unresponsive & have forcefulness quite out of it.

i've been writing the global array monthly_transactions. dateseperate runs fine long don't include lastly else statement. if that, monthly_transactions[0] returns array containing of jan invoices. issue arises in lastly else statement, when added, causes python freeze.

can help me shed lite on this?

i have written programme defines of arrays i'm going using (yes know global arrays aren't good. i'm marketer trying larn programming input give me on how improve much appreciated

import csv line_items = [] monthly_transactions = [] accounts_seperated = []

then import of info , place line_items array

def csv_dict_reader(file_obj): global board_info reader = csv.dictreader(file_obj, delimiter=',') line in reader: item = [] item.append(line["company id"]) item.append(line["user id"]) item.append(line["amount"]) item.append(line["transaction date"]) item.append(line["first transaction"]) line_items.append(item) if __name__ == "__main__": open("churntest.csv") f_obj: csv_dict_reader(f_obj) #formats transacation date info create more readable def dateformat(): in range(len(line_items)): ddmmyyyy =(line_items[i][3]) yyyymmdd = ddmmyyyy[6:] + "-"+ ddmmyyyy[:2] + "-" + ddmmyyyy[3:5] line_items[i][3] = yyyymmdd #takes line_items array , splits new array monthly_tranactions, each value holds 1 month of info def dateseperate(): in range(len(line_items)): #if there no values in monthly transactions, add together first line item if len(monthly_transactions) == 0: test = [] test.append(line_items[i]) monthly_transactions.append(test) # check see if line items year & month match value in monthly_transaction array. else: j in range(len(monthly_transactions)): line_year = line_items[i][3][:2] line_month = line_items[i][3][3:5] array_year = monthly_transactions[j][0][3][:2] array_month = monthly_transactions[j][0][3][3:5] #print(line_year, array_year, line_month, array_month) #if does, add together line item month if line_year == array_year , line_month == array_month: monthly_transactions[j].append(line_items[i]) #otherwise, create new sub array month else: monthly_transactions.append(line_items[i]) dateformat() dateseperate() print(monthly_transactions)

i really, appreciate thoughts or feedback guys give me on code.

based on comments on op, csv_dict_reader function seems want do, @ to the lowest degree inasmuch appends info argument csv file top-level variable line_items. said if print out line_items, shows info want.

"but appending doesn't work." take mean appending line_items monthly_transactions isn't beingness done. reason didn't tell programme it! appending you're talking done part of dateseparate function, still need phone call function.

i'm not sure how want utilize dateformat , dateseparate functions, in order utilize them, need include them in main function somehow calls, i.e. dateformat() , dateseparate().

edit: you've created potential endless loop in lastly else: section, extends monthly_transactions 1 if line/array year/month aren't equal. problematic because it's within loop for j in range(len(monthly_transactions)):. loop never end if length of monthly_transactions increased 1 every time through.

python arrays

No comments:

Post a Comment