작동하는 다음 코드를 만들었지 만 그것을 향상시키고 싶습니다. 파일을 다시 읽지는 않겠지 만 sales_input.seek (0)을 삭제하면 판매의 각 행을 반복 실행하지 않습니다. 어떻게 개선 할 수 있습니까?다시로드하지 않고 파이썬에서 csv 파일을 다시 읽음
def computeCritics(mode, cleaned_sales_input = "data/cleaned_sales.csv"):
if mode == 1:
print "creating customer.critics.recommendations"
critics_output = open("data/customer/customer.critics.recommendations",
"wb")
ID = getCustomerSet(cleaned_sales_input)
sales_dict = pickle.load(open("data/customer/books.dict.recommendations",
"r"))
else:
print "creating books.critics.recommendations"
critics_output = open("data/books/books.critics.recommendations",
"wb")
ID = getBookSet(cleaned_sales_input)
sales_dict = pickle.load(open("data/books/users.dict.recommendations",
"r"))
critics = {}
# make critics dict and pickle it
for i in ID:
with open(cleaned_sales_input, 'rb') as sales_input:
sales = csv.reader(sales_input) # read new
for j in sales:
if mode == 1:
if int(i) == int(j[2]):
sales_dict[int(j[6])] = 1
else:
if int(i) == int(j[6]):
sales_dict[int(j[2])] = 1
critics[int(i)] = sales_dict
pickle.dump(critics, critics_output)
print "done"
cleaned_sales_input 보이는 숫자 6이 책의 ID이며, 숫자 0이 나는 DICT 느릅 나무의 모양 싶어 고객 ID
입니다
6042772,2723,3546414,9782072488887,1,9.99,314968
6042769,2723,3546414,9782072488887,1,9.99,314968
...
같은
critics = {
CustomerID1: {
BookID1: 1,
BookID2: 0,
........
BookIDX: 0
},
CustomerID2: {
BookID1: 0,
BookID2: 1,
...
}
}
같은
또는
이 코드 패턴에것을 우선 보자 :
for i in ID:
for j in sales:
if int(i) == int(j[2])
통지 i
것을는 j[2]
와 비교되는 449,661,246,671,771,253,210
나는 여기
csv 읽기가 병목 현상인지 확인하려면 프로필을 작성 했습니까? – RickyA
미안하지만이 프로필이 무엇입니까? 나는 그것에 대해 들어 본 적이 없다. –
[profiler] (http://docs.python.org/2/library/profile.html)는 코드의 각 부분에 소요되는 시간을 확인하는 데 사용됩니다. 이를 통해 코드에서 병목 현상을 식별 할 수 있습니다. 병목 현상이 무엇인지 알지 못하기 때문에 프로필을 작성하기 전에 최적화 작업을 수행하면 거의 쓸모가 없습니다. 어쩌면 여기서 파일 판독이 병목 현상이 아닐 수도 있습니다. – RickyA