我使用了一个 .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
做错了什么?