为了账号安全,请及时绑定邮箱和手机立即绑定

TypeError:'str'对象不可调用(Python)

TypeError:'str'对象不可调用(Python)

慕妹3146593 2019-07-30 17:00:33
TypeError:'str'对象不可调用(Python)码:import urllib2 as uimport os as o inn = 'dword.txt'w = open(inn)z = w.readline()b = w.readline()c = w.readline()x = w.readline()m = w.readline()def Dict(Let, Mod):     global str     inn = 'dword.txt'     den = 'definitions.txt'     print 'reading definitions...'     dell =open(den, 'w')     print 'getting source code...'     f = u.urlopen('http://dictionary.reference.com/browse/' + Let)     a = f.read(800)     print 'writing source code to file...'     f = open("dic1.txt", "w")     f.write(a)     f.close()     j = open('defs.txt', 'w')     print 'finding definition is source code'     for line in open("dic1.txt"):         if '<meta name="description" content=' in line:            j.write(line)     j.close()     te = open('defs.txt', 'r').read().split()     sto = open('remove.txt', 'r').read().split()     print 'skimming down the definition...'     mar = []     for t in te:         if t.lower() in sto:             mar.append('')         else:              mar.append(t)     print mar     str = str(mar)     str = ''.join([ c for c in str if c not in (",", "'", '[', ']', '')])     defin = open(den, Mod)     defin.write(str)     defin.write('                 ')     defin.close()     print 'cleaning up...'     o.system('del dic1.txt')     o.system('del defs.txt')Dict(z, 'w')Dict(b, 'a')Dict(c, 'a')Dict(x, 'a')Dict(m, 'a')print 'all of the definitions are in definitions.txt'第一次Dict(z, 'w')工作然后第二次出现错误:Traceback (most recent call last):   File "C:\Users\test.py", line 64, in <module>     Dict(b, 'a')   File "C:\Users\test.py", line 52, in Dict     str = str(mar)TypeError: 'str' object is not callable有人知道为什么吗?
查看完整描述

3 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

这就是问题:

global str

str = str(mar)

你正在重新定义什么str()意思。str是字符串类型的内置Python名称,您不想更改它。

为局部变量使用不同的名称,并删除该global语句。


查看完整回答
反对 回复 2019-07-30
?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

虽然不在您的代码中,但另一个难以发现的错误是%在尝试字符串格式化时缺少该字符:

"foo %s bar %s coffee"("blah","asdf")

但它应该是:

"foo %s bar %s coffee"%("blah","asdf")

失踪%将导致相同的结果TypeError: 'str' object is not callable


查看完整回答
反对 回复 2019-07-30
?
慕标5832272

TA贡献1966条经验 获得超4个赞

另一种情况:使用非透明调用失败__repr__的对象的功能进行混乱format()

在我们的例子中,我们使用了一个@property装饰器__repr__并将该对象传递给了一个format()。该@property装饰使__repr__被打开成一个字符串,其然后导致对象str对象是不可调用错误。


查看完整回答
反对 回复 2019-07-30
  • 3 回答
  • 0 关注
  • 1907 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信