2013-01-06 23 views
3

我使用.txt文件這一點,並在每行看起來如下:如何正確創建自己的模塊,在其中導入另一個模塊?

1/2/09,902,898.5,905.25,913.25,914.5,914.25,915.5,924.5,926.25,929.25,927.5,926 

大概有50條這樣的線路中的.txt文件。

這裏是我做的代碼:

class PriceProcesseor(object): 

    import csv 
    import datetime 

    def __init__(self, text_file_with_prices): 
     self.text_file_with_prices = text_file_with_prices 
     self.file_each_date_is_a_list_of_strings = self.__turn_file_into_list() 

    def __turn_file_into_list(self): 
     self.text_file_with_prices1 = open(self.text_file_with_prices, 'rU') 
     self.text_file_with_prices2 = csv.reader(self.text_file_with_prices1) 
     return self.text_file_with_prices2 

    def file_as_list(self): 
     '''Returns the file as a list of strings''' 
     self.whole_file_as_list_of_lists = [] 
     for x in self.file_each_date_is_a_list_of_strings: 
      self.whole_file_as_list_of_lists.append(x) 
     return self.whole_file_as_list_of_lists 

    # Make a method that can return any given line 

    def return_line(self,line_number): 
     self.line_number = line_number 
     return self.file_as_list()[line_number] 

    def the_works(self): 
     self.the_works_list = [] 
     for date in self.file_as_list(): 
      self.mini_list = [] 
      for item in date: 
       if '/' in item: 
        self.date = str(datetime.datetime.strptime(item,'%m/%d/%y')) 
        self.date_munged = self.date.strip('00:00:00') 
        self.date_munged_1 = self.date_munged.strip() 
        self.mini_list.append(self.date_munged_1) 
       elif '/' not in item: 
        self.mini_list.append(float(item)) 
      self.the_works_list.append(self.mini_list) 
     return self.the_works_list 

但是,當我這樣做:

my_file = '/directory1/directory2/the_file.txt' 
xyz = PriceProcesseor(my_file) 
xyz.the_works() 

我得到的錯誤信息:

NameError: global name 'csv' is not defined 

正在採取哪些錯誤?

回答

3

import語句放在類定義之外。

import csv 
import datetime 

class PriceProcesseor(object): 
#etc. 

該類塊創建一個範圍。當您在該範圍內導入東西時,它們只能在該範圍內訪問。類作用域不能直接被類的方法訪問,所以它們看不到導入的模塊。

+0

你說得對,這絕對是合適的解決方案。由於方法*可以通過「PriceProcesseor.whatever」或「self.__ class __。whatever」來間接訪問類作用域,所以你可能會說「類作用域不是*可直接訪問」。 – mgilson

+0

哇愚蠢的,謝謝。所以只是爲了重述一下你所說的話,我應該把導入語句放到他們現在所在的.py文件中,除非把它們放到類中,就像我擁有它們一樣,但是放在類的頂部。 – user1367204

1

所以做的正確的方法是如下:

import csv 
import datetime 

class PriceProcesseor(object): 


    def __init__(self, text_file_with_prices): 
     self.text_file_with_prices = text_file_with_prices 
     self.file_each_date_is_a_list_of_strings = self.__turn_file_into_list() 

    def __turn_file_into_list(self): 
     self.text_file_with_prices1 = open(self.text_file_with_prices, 'rU') 
     self.text_file_with_prices2 = csv.reader(self.text_file_with_prices1) 
     return self.text_file_with_prices2 

    def file_as_list(self): 
     '''Returns the file as a list of strings''' 
     self.whole_file_as_list_of_lists = [] 
     for x in self.file_each_date_is_a_list_of_strings: 
      self.whole_file_as_list_of_lists.append(x) 
     return self.whole_file_as_list_of_lists 

    # Make a method that can return any given line 

    def return_line(self,line_number): 
     self.line_number = line_number 
     return self.file_as_list()[line_number] 

    def the_works(self): 
     self.the_works_list = [] 
     for date in self.file_as_list(): 
      self.mini_list = [] 
      for item in date: 
       if '/' in item: 
        self.date = str(datetime.datetime.strptime(item,'%m/%d/%y')) 

接下來,我將文件保存爲ABC123.py。當我輸入導入ABC123時,一切都將完美工作。

相關問題