2014-05-03 82 views
0

很新的python,我試圖創建一個小函數,旨在將一個字符串的給定數量的實例寫入文件(例如作爲一個詞雲工具)。我已經嘗試使用下面的類來完成此操作,但出於某種原因,不會收到任何輸出,但也沒有錯誤消息。使用類向文件寫入字符串 - 空輸出

我想也許我沒有正確聲明count作爲整數作爲輸入?但是,不顯示錯誤消息,這使得它有點令人困惑。

再次,對Python很新,所以任何幫助以及一些解釋將不勝感激:)代碼如下!

#Prints multiple instances of a string as input for a word cloud tool 


class print_list(object): 
    wordlist = [] 
    def __init__(int(count), word, self): 
     self.count = count 
     self.word = word 

    def write_list(self): 
     while count > 0: 
      wordlist.append(word) 
      print word + "\n" 
      count = count - 1 
      return wordlist 

    def write_file(self): 
     my_file = open("cloud.txt", "w") 
     for word in wordlist: 
      my_file.write(word + "\n") 
     my_file.close   


Python = print_list(10, "Python") 

回答

1

您有很多語法錯誤。首先,self需要先來,類型轉換不會發生在函數定義中。所以你__init__應該像

def __init__(self, count, word): 
     self.count = int(count) 
     self.word = word 

其次,所有的屬性,像wordlistcountword需要爲self.wordlist被訪問,self.word等方法裏面。因此,例如,write_file

def write_file(self): 
    my_file = open("cloud.txt", "w") 
    for word in self.wordlist: 
     my_file.write(word + "\n") 
    my_file.close 

而且write_list應該

def write_list(self): 
    while self.count > 0: 
     self.wordlist.append(self.word) 
     print self.word + "\n" 
     self.count = self.count - 1 
    return self.wordlist 

(我也未縮進return語句使循環實際上得到執行,但我認爲這是一個複製到stackexchange錯誤)。

最後,你不打電話給你的任何方法,如填寫wordlist並寫它。所以爲了讓你的班級真正寫出文件,你需要調用write_file方法。對您的代碼進行這些更改,我們有:

#Prints multiple instances of a string as input for a word cloud tool 


class print_list(object): 
    wordlist = [] 
    def __init__(self, count, word): 
     self.count = count 
     self.word = word 

    def write_list(self): 
     while self.count > 0: 
      self.wordlist.append(self.word) 
      print self.word + "\n" 
      self.count = self.count - 1 
     return self.wordlist 

    def write_file(self): 
     my_file = open("cloud.txt", "w") 
     for word in self.wordlist: 
      my_file.write(word + "\n") 
     my_file.close() 


Python = print_list(10, "Python") 
Python.write_list() 
Python.write_file() 
+0

很好,非常感謝您的詳細解釋;你的解決方案對我來說工作得很好! – Matthias

1
class PrintList(object): 
    def __init__(self,count, word):# self comes first in your init method 
     self.count = count 
     self.word = word # use self to refer to instance attributes 
     self.wordlist=[] # make wordlist an attribute 


    def write_list(self): 
     while self.count > 0: 
      self.wordlist.append(self.word) 
      print self.word + "\n" 
      self.count -= 1 
     return self.wordlist 

    def write_file(self): 
     with open("cloud.txt", "w") as my_file: # using with automatically closes the file 
      for word in self.wordlist: 
       my_file.write(word + "\n") 


pl = PrintList(10, "Python") # create instance of Printlist class 
print pl.write_list() 
pl.write_file()# need to call method on instance to write to the file 

Python classes

+0

謝謝,工作正常!非常感謝! – Matthias

+0

不客氣。 –

1

首先,我沒有得到一個語法錯誤...

class print_list(object): 
    #int(count) isn't necessary and causes Syntax Error on my python 2.7 
    def __init__(self, count, word): 
     self.count = count 
     self.word = word 
     #I think wordlist will be more useful here, as a class attribute (1) 
     self.wordlist = [] 

    def write_list(self): 
     #I copy the self.count to a local variable (2) 
     countCopy = self.count 
     while count > 0: 
      self.wordlist.append(self.word) 
      print self.word + "\n" 
      countCopy = countCopy - 1 

    def write_file(self): 
     #nitpick: hardcoding filenames is bad practice, it would be better to pass it as an argument 
     my_file = open("cloud.txt", "w") 
     #self.wordlist is the list built in the above function 
     #so if this function is called first, self.wordlist is an empty list 
     for word in self.wordlist: 
      my_file.write(word + "\n") 
     #you forgot parentheses below 
     my_file.close() 

#More code here... (3) 

備註:

  1. 單詞表製作類屬性允許你保持曾經的建造d列表中,並且可以通過其他類方法輕鬆訪問,例如write_file

  2. 得益於此,self.count__init__被調用時起保持不變。

  3. 只需實例化該類將不會調用我們定義的所有方法。這意味着,除了tool = print_list(10, "word")之外,您還必須調用每種方法。順便說一句,「巨蟒」是一個不好的名字一類...

,一般性的評論:

你似乎感到困惑,什麼應該是一個類屬性(EI self.x代替x)和什麼應該是一個局部變量。以及如何使用這些。 Python在默認情況下查找本地變量,但不查找類屬性。如果你想訪問一個班級屬性,你必須前綴其名稱self.。否則,你會得到一個NameError或只是一個錯誤的結果。

+0

謝謝,您的解決方案非常棒,您的意見非常有幫助;從這一個中吸取了很多教訓! – Matthias