我尝试使用文件中的数据提取并使用它创建对象。
数据示例:ACB,20170327,0.058,0.058,0.058,0.058,175116
这是数据的格式: stock_code,日期,opening_price,HIGH_PRICE,LOW_PRICE,closing_price,体积
我试图创建一个像这样的对象:
stock_code = TradingData(date,opening_price,high_price,low_price,closing_price,volume)
这是我到目前为止所得到的
class TradingData(object) :
"""Stock market data for a single day of trading for one stock.
The trading data includes the:
Date of trading
Value of opening (first) trade
Value of highest trade
Value of lowest trade
Value of closing (final) trade
Volume of shares traded
"""
def __init__(self, date, day_open, day_high, day_low, day_close, volume):
"""
Parameters:
date (str): Date in yyyymmdd format.
day_open (float): Dollar value of the first trade of the day.
day_high (float): Dollar value of the highest trade of the day.
day_low (float): Dollar value of the lowest trade of the day.
day_close (float): Dollar value of the last trade of the day.
volume (int): The number of shares traded on this day.
"""
self._date = date
self._open = day_open
self._high = day_high
self._low = day_low
self._close = day_close
self._volume = volume
def get_date(self) :
"""(str) The date of this day of trading."""
return self._date
def set_date(self, date) :
self._date = date
def get_open(self) :
"""(float) Value of the opening trade of the day."""
return self._open
def set_open(self, day_open) :
self._open = day_open
def get_high(self) :
"""(float) Value of highest trade of the day."""
return self._high
def set_high(self, day_high) :
self._high = day_high
def get_low(self) :
"""(float) Value of lowest trade of the day."""
return self._low
def set_low(self, day_low) :
self._low = day_low
def get_close(self) :
"""(float) Value of final trade of the day."""
return self._close
def set_close(self, day_close) :
self._close = day_close
def get_volume(self) :
"""(int) Value of highest trade of the day."""
return self._volume
def set_volume(self, volume) :
self._volume = volume
class Loader(object) :
"""Abstract class defining basic process of loading trading data."""
def __init__(self, filename, stocks) :
"""Data is loaded on object creation.
Parameters:
filename (str): Name of the file from which to load data.
stocks (StockCollection): Collection of existing stock market data
to which the new data will be added.
"""
self._stocks = stocks
with open(filename, "r") as file :
self._process(file)
def _process(self, file) :
"""Load and parse the stock market data from 'file'."""
raise NotImplementedError()
class LoadCSV(Loader):
def __init__(self, filename, stocks):
super().__init__(filename, stocks)
def _process(self, file):
for stock in file:
stock.split(',')[0] = stocks.TradingData(stock.split(',')[1],
stock.split(',')[2],
stock.split(',')[3],
stock.split(',')[4],
stock.split(',')[5],
stock.split(',')[6])
print(stocks.split(',')[0].TradingData())
答案 0 :(得分:1)
for stock in file:
parse_trading_data(stock)
def parse_trading_data(stock):
str_arr = stock.split(',')
data = {
"date": str_arr[0],
...
}
return TradingData(data)
这样的东西?