如何正确制作我自己的模块,导入其中的另一个模块?

时间:2013-01-06 03:55:47

标签: python class csv module python-2.7

我使用.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

.txt文件中可能有50行。

以下是我制作的代码:

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

做错了什么?

2 个答案:

答案 0 :(得分:3)

import语句放在类定义之外。

import csv
import datetime

class PriceProcesseor(object):
#etc.

类块创建范围。导入该范围内的内容时,只能在该范围内访问它们。类的方法不能直接访问类的范围,因此它们无法看到导入的模块。

答案 1 :(得分: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时,一切都会完美。