您好,我希望有人能幫助我完成大學課程,我的代碼有問題。我一直在運行數據導出時遇到內存錯誤。將數據導出到csv文件時出現內存錯誤
有什麼辦法可以減少正在使用的內存,或者我可以採用不同的方法嗎?
對於課程工作,我從一個CSV文件中獲得一個關於客戶訂單的300條記錄文件,然後我必須將星期五記錄導出爲新的CSV文件。此外,我需要打印客戶訂單最流行的方法和訂單中籌集的資金,但我有一個簡單的計劃。
這是我第一次使用CSV,所以我不知道該怎麼做。當我運行程序時,它往往會立即崩潰或停止響應。一旦它出現'MEMORY ERROR',但它就是它的全部。我使用的是大學提供的電腦,所以我不確定具體的規格,但我知道它運行4GB的內存。
限定計數OCCURENCES預定函數
def countOccurences(target,array):
counter = 0
for element in array:
if element == target:
counter= counter + 1
print counter
return counter
爲用於從設置文件中收集數據
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文件
程序 DataInput中函數創建用戶定義的函數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)
首先你可以添加確切的錯誤信息,你正在得到?第二次檢查您的計算機上是否有足夠的磁盤空間。 – DrBwts
在對問題的描述中,我提到了它的出現。它出現只是'內存錯誤' –
另一方面...你沒有正確地關閉你的文件。你應該使用'open()'來自動關閉文件或者使用'f.close()'(括號)。第一個函數('countOccurences')是多餘的。如果數組是一個列表,你可以使用'list.count'方法 –