1

Hello I was hoping someone could help me with my college coursework, I have an issue with my code. I keep running into a memory error with my data export.

Is there any way I can reduce the memory that is being used or is there a different approach I can take?

For the course work I am given a file of 300 records about customer orders from a CSV file and then I have to export the Friday records to a new CSV file. Also I am required to print the most popular method for customer's orders and the total money raised from the orders but I have an easy plan for that.

This is my first time working with CSV so I'm not sure how to do it. When I run the program it tends to crash instantly or stop responding. Once it appeared with 'MEMORY ERROR' however that is all it appeared with. I'm using a college provided computer so I am not sure on the exact specs but I know it runs 4GB of memory.

defining count occurences predefined function

def countOccurences(target,array):
    counter = 0
    for element in array:
        if element == target:
            counter= counter + 1
    print counter
    return counter

creating user defined functions for the program dataInput function used for collecting data from provided file

def dataInput():
    import csv
    recordArray = []
    customerArray = []

    f = open('E:\Portable Python 2.7.6.1\Choral Shield Data File(CSV).csv')
    csv_f = csv.reader(f)

    for row in csv_f:
        customerArray.append(row[0])
        ticketID = row[1]
        day, area = datasplit(ticketID)
        customerArray.append(day)
        customerArray.append(area)
        customerArray.append(row[2])
        customerArray.append(row[3])
        recordArray.append(customerArray)
    f.close
    return recordArray

def datasplit(variable):
        day = variable[0]
        area = variable[1]
        return day,area

def dataProcessing(recordArray):
    methodArray = []
    wed_thursCost = 5
    friCost = 10

    record = 0
    while record < 300:
        method = recordArray[record][4]
        methodArray.append(method)
        record = record+1

    school = countOccurences('S',methodArray)
    website = countOccurences('W',methodArray)

    if school > website:
        school = True
    elif school < website:
        website = True

    dayArray = []
    record = 0
    while record < 300:
        day = recordArray[record][1]
        dayArray.append(day)
        record = record + 1

    fridays = countOccurences('F',dayArray)
    wednesdays = countOccurences('W',dayArray)
    thursdays = countOccurences('T', dayArray)

    totalFriCost = fridays * friCost
    totalWedCost = wednesdays * wed_thursCost
    totalThurCost = thursdays * wed_thursCost
    totalCost = totalFriCost + totalWedCost + totalThurCost

    return totalCost,school,website

My first attempt to writing to a csv file

def dataExport(recordArray):
    import csv
    fridayRecords = []
    record = 0
    customerIDArray = []
    ticketIDArray = []
    numberArray = []
    methodArray = []


    record = 0
    while record < 300:
        if recordArray[record][1] == 'F':
            fridayRecords.append(recordArray[record])
            record = record + 1

    with open('\Courswork output.csv',"wb") as f:
        writer = csv.writer(f)
        for record in fridayRecords:
            writer.writerows(fridayRecords)
        f.close

My second attempt at writing to the CSV file

def write_file(recordArray): # write selected records to a new csv file
    CustomerID = []
    TicketID = []
    Number = []
    Method = []
    counter = 0
    while counter < 300:
        if recordArray[counter][2] == 'F':
            CustomerID.append(recordArray[counter][0])
            TicketID.append(recordArray[counter][1]+recordArray[counter[2]])
            Number.append(recordArray[counter][3])
            Method.append(recordArray[counter][4])
    fridayRecords = [] # a list to contain the lists before writing to file
    for x in range(len(CustomerID)):
        one_record = CustomerID[x],TicketID[x],Number[x],Method[x]
        fridayRecords.append(one_record) 
    #open file for writing
    with open("sample_output.csv", "wb") as f: 
 #create the csv writer object
        writer = csv.writer(f)
        #write one row (item) of data at a time
        writer.writerows(recordArray)
    f.close
    counter = counter + 1

#Main Program

recordArray = dataInput()
totalCost,school,website = dataProcessing(recordArray)
write_file(recordArray)
8
  • first off can you add the exact error message you are getting? 2nd check you have enough disk space on your computer. Commented Mar 9, 2017 at 15:06
  • In the description of the problem I mentioned what it appears with. It appeared with just 'Memory Error' Commented Mar 9, 2017 at 15:10
  • As an aside... you aren't closing your files properly. You should use with open() to automatically close the file or use f.close() (with brackets). And the first function (countOccurences) is redundant. If the array is a list, you can use the list.count method Commented Mar 9, 2017 at 15:30
  • This is for my college coursework so I have to show an understanding of standard algorithms Commented Mar 9, 2017 at 15:34
  • Do you know which function causes the memory error? Have you tried calling them one by one to find out which one it is? Commented Mar 9, 2017 at 16:01

1 Answer 1

1

In the function write_file(recordArray) in your second attempt the counter variable counter in the first while loop is never updated so the loop continues for ever until you run out of memory.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I thought that would work but I have just tried that and it still seems to run out of memory. I added counter = counter + 1 just at the end of the while loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.