python加载csv文件去重_重新读取python中的csv文件而不重新加载它

我做了以下代码,但我想改进它。我不想重读该文件,但如果我删除sales_输入.搜索(0)它不会在sales中迭代抛出每一行。我该如何改进?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"

干净的销售输入看起来像

^{pr2}$

其中数字6是图书ID,数字0是客户ID

我想要一个看起来像critics = {

CustomerID1: {

BookID1: 1,

BookID2: 0,

........

BookIDX: 0

},

CustomerID2: {

BookID1: 0,

BookID2: 1,

...

}

}

或者critics = {

BookID1: {

CustomerID1: 1,

CustomerID2: 0,

........

CustomerIDX: 0

},

BookID1: {

CustomerID1: 0,

CustomerID2: 1,

...

CustomerIDX: 0

}

}

我希望这不是很多信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值