2

在我的代码中,我试图从文件中提取一些数据。当我尝试在第 61 行运行我的代码时出现此错误。我的代码在这里:

from datetime import date
from math import floor;
from adjset_builder import adjset_builder
def check_and_update(d,start,end,log):    
#    print start,end;
    if start in d:
        if end in d[start]:
            log.write("{0}-{1}\n".format(start, end))
            if d[start][end] == 1:
                print "one found"
            d[start][end] += 1

def build_dictionary(my_adjset,my_list,factor,outfile,log):
    log.write("building dictionary:\n");

    window_size = int(floor(len(my_list)*factor));
    if window_size<2:
        log.write("too small\n")
        return;   
    log.write('Total:{0},windowsize:{1}\n'.format(len(my_list),window_size));
    log.write("Window at place: 0,")
    for i in xrange(window_size):
        j = i+1;
        while j<window_size:
            check_and_update(my_adjset, my_list[i][1], my_list[j][1],log);
            j=j+1

    i=1;
    while i<=len(my_list)-window_size:
        log.write("{0},".format(i)) 
        j=i;
        k=i+window_size-1;
        while j<k:
            check_and_update(my_adjset, my_list[i][1], my_list[j][1],log);  
            j+=1
        i += 1  
    log.write("\nDictionary building done\n")        

def make_movie_list(infilename,factor):
    log=open('log.txt','w');
    outfile=open(infilename.split('.')[0]+"_plot_"+str(factor)+".txt",'w');
    f=open(infilename,'r');
    my_adjset=dict()
    adjset_builder('friends.txt', my_adjset);

    count =1
    while True:        
        string = f.readline();
        if string=='':
            break;        
        log.write("count:{0}\n".format(count))
        count += 1

        [movie,freunde] = string.split('=');
        freunde = freunde.split(';')
        mylist=[]
        for i in freunde:
            [user_id,date] = i.split(' ');
            [yy,mm,dd] = date.split('-');  
#            l=list((date(int(yy),int(mm),int(dd)),user_id))
            mylist.append([date(int(yy),int(mm),int(dd)),user_id]); ## line 61         
        log.write("list built");   
        print mylist         
        break;    
#        build_dictionary(my_adjset, mylist, factor,outfile,log) 
        del(mylist);   

    print 'Done'

    log.close();
    outfile.close();
    f.close();
    print count 


if __name__ == '__main__':    
    make_movie_list('grades_processed.txt',.5)

但是,当我尝试在“控制台”中模拟相同的东西时,我没有收到任何错误:

dd='12'
mm='2'
yy='1991'
user_id='98807'  
from datetime import date
from datetime import date
l=list((date(int(yy),int(mm),int(dd)),user_id))
l [datetime.date(1991, 2, 12), '98807']

可能是非常愚蠢的事情,但我是初学者,所以似乎没有注意到错误。谢谢!

4

2 回答 2

6

这产生date了一个功能:

from datetime import date

这会产生date一个字符串:

        [user_id,date] = i.split(' ');

TypeError现在得到一个,因为date不再是一个函数:

mylist.append([date(int(yy),int(mm),int(dd)),user_id]); 

避免此错误的一种方法是导入模块而不是函数

import datetime as dt
mylist.append([dt.date(int(yy),int(mm),int(dd)),user_id])

或更简洁地说,

mylist.append([dt.date(*date.split('-')), user_id])

PS:删除所有不必要的分号!

于 2012-05-27T16:40:54.877 回答
5

您有一个名为 date 的变量,将其重命名,使其不会影响 datetime 中的 date 函数。

于 2012-05-27T16:39:15.530 回答