TypeError:'str'對(duì)象不可調(diào)用(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')工作然后第二次出現(xiàn)錯(cuò)誤: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貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊
這就是問題:
global str str = str(mar)
你正在重新定義什么str()
意思。str
是字符串類型的內(nèi)置Python名稱,您不想更改它。
為局部變量使用不同的名稱,并刪除該global
語句。

搖曳的薔薇
TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊
雖然不在您的代碼中,但另一個(gè)難以發(fā)現(xiàn)的錯(cuò)誤是%
在嘗試字符串格式化時(shí)缺少該字符:
"foo %s bar %s coffee"("blah","asdf")
但它應(yīng)該是:
"foo %s bar %s coffee"%("blah","asdf")
失蹤%
將導(dǎo)致相同的結(jié)果TypeError: 'str' object is not callable
。

慕標(biāo)5832272
TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊
另一種情況:使用非透明調(diào)用失敗__repr__
的對(duì)象的功能進(jìn)行混亂format()
。
在我們的例子中,我們使用了一個(gè)@property
裝飾器__repr__
并將該對(duì)象傳遞給了一個(gè)format()
。該@property
裝飾使__repr__
被打開成一個(gè)字符串,其然后導(dǎo)致對(duì)象str
對(duì)象是不可調(diào)用錯(cuò)誤。
添加回答
舉報(bào)
0/150
提交
取消