码:
import urllib2 as u
import 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'
Run Code Online (Sandbox Code Playgroud)
第一次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
Run Code Online (Sandbox Code Playgroud)
有人知道为什么吗?
@Greg Hewgill:
我已经尝试过了,我得到了错误:
Traceback (most recent call last):
File "C:\Users\test.py", line 63, in <module>
Dict(z, 'w')
File "C:\Users\test.py", line 53, in Dict
strr = ''.join([ c for c in str if c not in (",", "'", '[', ']', '')])
TypeError: 'type' object is not iterable
Run Code Online (Sandbox Code Playgroud)
n61*_*007 106
虽然不在您的代码中,但另一个难以发现的错误是%在尝试字符串格式化时缺少该字符:
"foo %s bar %s coffee"("blah","asdf")
Run Code Online (Sandbox Code Playgroud)
但它应该是:
"foo %s bar %s coffee"%("blah","asdf")
Run Code Online (Sandbox Code Playgroud)
失踪%将导致相同的结果TypeError: 'str' object is not callable.
Gre*_*ill 105
这就是问题:
global str
str = str(mar)
Run Code Online (Sandbox Code Playgroud)
你正在重新定义什么str()意思.str是字符串类型的内置Python名称,您不想更改它.
为局部变量使用不同的名称,并删除该global语句.
另一种情况:使用非透明调用失败__repr__的对象的功能进行混乱format().
在我们的例子中,我们使用了一个@property装饰器__repr__并将该对象传递给了一个format().该@property装饰使__repr__被打开成一个字符串,其然后导致对象str对象是不可调用错误.
小智 6
重要的是要注意(如果您是Google来的,请注意)“ TypeError:'str'对象不可调用 ”意味着仅尝试将先前声明为String-type的变量用作函数(例如,通过最后添加括号。)
如果您使用任何其他内置方法作为变量名称,则也可以获得完全相同的错误消息。
小智 5
我有同样的错误。就我而言,不是因为名为 的变量str。但是因为我命名的函数带有str参数并且变量相同。
same_name = same_name(var_name: str)
Run Code Online (Sandbox Code Playgroud)
我循环运行它。第一次运行没问题。我第二次遇到这个错误。将变量重命名为与函数名称不同的名称修复了此问题。所以我认为这是因为Python一旦在作用域中关联了一个函数名称,第二次尝试将左侧部分(same_name =)关联为对函数的调用,并检测到str参数不存在,因此它丢失了,然后抛出该错误。
str = 'Hello World String'
print(str(10)+' Good day!!')
Run Code Online (Sandbox Code Playgroud)
即使我在上面的代码中也遇到了这个问题,因为我们正在隐藏str()函数。
解决办法是:
string1 = 'Hello World String'
print(str(10)+' Good day!!')
Run Code Online (Sandbox Code Playgroud)