将数据导出到csv文件时出现内存错误

时间:2017-03-09 15:01:39

标签: python-2.7 python-3.x

您好我希望有人可以帮助我完成我的大学课程,我的代码存在问题。我的数据导出时一直遇到内存错误。

有什么方法可以减少正在使用的内存,或者我可以采用不同的方法吗?

对于课程作业,我从CSV文件中获得了300条关于客户订单记录的文件,然后我必须将星期五记录导出到新的CSV文件。此外,我需要打印最流行的客户订单方法和订单总收​​入,但我有一个简单的计划。

这是我第一次使用CSV,所以我不知道该怎么做。当我运行程序时,它会立即崩溃或停止响应。一旦它出现'MEMORY ERROR',那就是它出现的全部。我正在使用大学提供的计算机,所以我不确定具体的规格,但我知道它运行4GB内存。

定义计数发生预定义函数

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

为程序创建用户定义的函数 dataInput函数用于从提供的文件中收集数据

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

我第一次尝试写入csv文件

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

我第二次尝试写入CSV文件

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)

1 个答案:

答案 0 :(得分:1)

在第二次尝试的函数write_file(recordArray)中,第一个counter循环中的计数器变量while永远不会更新,因此循环将一直持续到内存不足为止。