【发布时间】:2017-03-09 15:01:38
【问题描述】:
您好,我希望有人可以帮助我完成大学课程,但我的代码有问题。我在导出数据时一直遇到内存错误。
有什么方法可以减少正在使用的内存,或者我可以采取其他方法吗?
对于课程作业,我从 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)
【问题讨论】:
-
首先你能添加你得到的确切错误信息吗?第二次检查您的计算机上有足够的磁盘空间。
-
在问题的描述中我提到了它的出现。它只显示“内存错误”
-
顺便说一句...您没有正确关闭文件。您应该使用
with open()自动关闭文件或使用f.close()(带括号)。第一个函数 (countOccurences) 是多余的。如果数组是列表,可以使用list.count方法 -
这是我的大学课程,所以我必须表现出对标准算法的理解
-
你知道是哪个函数导致了内存错误吗?你有没有试过一一打电话给他们看看是哪一个?
标签: python-2.7 python-3.x