2011-05-18 77 views
39

代碼:類型錯誤: 'STR' 對象不是可調用(Python)的

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' 

第一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 

有人知道爲什麼嗎?

@格雷格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 
+4

重命名您的'str'變量的所有實例,包括53行中該錯誤正在抱怨的那個實例。 – 2011-05-18 04:47:47

回答

79

這就是問題所在:

global str 

str = str(mar) 

您正在重新定義什麼str()手段。 str是字符串類型的內置Python名稱,您不想更改它。

爲本地變量使用不同的名稱,並刪除global語句。

+6

並不意味着被使用,但是'str()'函數也可以用作__builtins __。str()'。當然,在99.9999%的案例中使用它是一個糟糕的主意,我想。 – n611x007 2013-09-26 20:23:35

+0

請記住,您需要重新啓動python內核才能返回「str()」功能。 – yeliabsalohcin 2017-09-04 09:48:36

85

雖然不是在你的代碼,當%字符的字符串格式化的嘗試缺少另一個難以當場錯誤是:

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

,但它應該是:

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

缺少%將導致相同的TypeError: 'str' object is not callable

+4

謝謝先生!簡單的錯誤,但很大程度上影響了破壞腳本。 – ChrisR 2014-01-13 19:27:17

+0

我知道這真的很舊,但我剛剛遇到了這個問題,並且這個答案有幫助,謝謝@naxa – Bbit 2015-05-31 00:12:15

+0

這經過了一小時的故障排除後才救了我。謝謝! – Michael 2016-12-19 18:54:43

12

在我的情況下,我有一個類有一個方法和一個字符串屬性的同名,我試圖調用該方法,但獲取字符串屬性。

4

這種情況的另一種情況:與對象的__repr__函數混淆,其中format()調用不透明地失敗。

在我們的案例中,我們在__repr__上使用了一個@property修飾器,並將該對象傳遞給format()。裝飾器@property導致__repr__對象被轉換爲字符串,然後導致str對象不可調用錯誤。

+1

謝謝,這也是我的問題(我沒有使用格式,但出於某種原因,在'__repr__'上有一個@property) – Justin 2016-05-17 18:01:24

+0

這個解決方案應該是真正高效的。 – 2018-02-09 20:09:58

0

我有同樣的錯誤。在我的情況下,因爲名爲str的變量而不是。但是因爲我用str參數命名了一個函數,並且這個變量也是一樣的。

same_name = same_name(var_name:str)

我在循環中運行它。第一次運行正常。第二次我得到這個錯誤。將該變量重命名爲與函數名稱不同的名稱解決了此問題。所以我認爲這是因爲Python曾經將一個函數名稱關聯到一個作用域中,第二次嘗試將左邊部分(same_name =)作爲對該函數的調用進行關聯,並檢測到str參數不存在,那麼它會拋出該錯誤。

-1

在我的情況下,我有一個類中有一個方法。該方法沒有「self」作爲第一個參數,當我調用該方法時拋出錯誤。一旦我將「self」添加到方法的參數列表中,就沒有問題。

2

如果您有變量str並試圖調用str()函數,則可以得到此錯誤。

0

一個問題我剛剛不小心被調用字符串

"Foo" ("Bar" if bar else "Baz") 

你可以只是把它們彼此相鄰,像這樣

"Foo" "Bar" 

但是因爲在開括號的串聯串第一個例子,它認爲我試圖呼叫"Foo"

0

它可能也是你試圖以錯誤的方式索引:

a = 'apple' 
a(3) ===> 'str' object is not callable 

a[3] = l 
相關問題