2016-10-04 8 views
-1

消える私はこのような応答のクラスを定義します。Pythonのクラスのインスタンス変数が

class Response(object): 
    def __index__(self): 
     self.country = "" 
     self.time_human = "" 
     self.time_utc = "" 
     self.text = "" 
     self.time_object = None 
     self.clean_word_list = [] 

    def parse_line(self, line): 
     if 'text:' in line[:10]: 
      self.text = line[7:].strip() 
     elif 'country: ' in line[:9]: 
      self.country = line[8:].strip() 
     elif 'time_human: ' in line[:15]: 
      self.time_human = line[12:].strip() 
     elif 'time_utc: ' in line[:15]: 
      self.time_utc = int(line[10:].strip()) 
      self.time_object = datetime.fromtimestamp(self.time_utc) 

私は、テキストファイルから行を読み取り、応答に適切な値を代入する方法があります:

class file_importer(object): 
    def __init__(self, file_name): 
     self.file_name = file_name 

def get_responses_from_file(self): 
    directory = DIRECTORY_TO_FILE 
    formatted_filename = directory + self.file_name 
    file = open(formatted_filename, 'r') 
    response = Response() 
    response_list = [] 
    for line in file: 
     if line[0] == '*': 
      response_list.append(response) 
      response = Response() 
     else: 
      response.parse_line(line) 
    return response_list 

しかし、get_responses_from_file()が返すresponse_listは、response.clean_word_list属性を持たないレスポンスのリストです。何が起こった?

+0

である必要があります**' __index__' – Wombatz

答えて

0

それはコンストラクタが `__init__`と**ではないと呼ばれている__init__の代わりに、あなたのResponseクラスの__index__

+0

ありがとう!私は絶対にその行を誤解しました。ドラキュラの色のテーマと睡眠不足の組み合わせは、悪いコンボです。私にそれを指摘してくれてありがとう – Ian

関連する問題