'str'对象不可调用TypeError

时间:2012-05-27 16:33:18

标签: python python-2.7

在我的代码中,我试图从文件中提取一些数据。当我尝试在第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']

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

2 个答案:

答案 0 :(得分:5)

这使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 modules instead of functions

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:删除所有不必要的分号!

答案 1 :(得分:4)

你有一个名为date的变量,将其重命名,以便它不会影响日期时间的日期函数。