本文介绍了'str' 对象不可调用 TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my code I am trying to extract some data from a file. I am getting this error when I am trying to run my code on line 61. My code here:

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)

然而,当我尝试在控制台"中模拟同样的事情时,我没有收到任何错误:

However when I tried to simulate the same thing in 'Console' I do not get any error:

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']

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

Might be something very silly but I am a beginner so can not seem to notice the mistake. Thank you!

推荐答案

这使得 date 成为一个函数:

This makes date a function:

from datetime import date

这使得 date 成为一个字符串:

This makes date a string:

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

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

You get a TypeError now, since date is no longer a function:

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

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

One way to avoid this error is to 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:删除所有不必要的分号!

PS: Remove all those unnecessary semicolons!

这篇关于'str' 对象不可调用 TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:18