读取文件后,Python 3.6空字典更改为TextIOWrapper

时间:2018-07-10 11:37:00

标签: python-3.x dictionary types

我已声明一个空的dict,以后再填充包含股市数据的元组列表,而对于我自己,我无法弄清楚为什么当我四处分配时,“ dict”变成了“ TextIOWrapper”列表

这是我的代码:

months = ('jan', 'feb', 'mar', 'apr', 'may', 'jun',
          'jul', 'aug', 'sep', 'oct', 'nov', 'dec')
path = os.getcwd()
ohlc_path = os.path.join(path, 'ohlc')

data = dict()
print('data type: %s' % type(data))
ctr = 0
for m in months:
    filepath = os.path.join(ohlc_path, 'BANKNIFTY' + m + '.txt')
    temp = list()
    with open(filepath, 'r') as data:
        for line in data:
            ctr += 1
            raw = (line.strip().split(','))
            ts = datetime.strptime(raw[1] + '-' + raw[2], '%Y%m%d-%H:%M')
            fin = (str(raw[0]), ts, float(raw[3]),
                   float(raw[4]), float(raw[5]), float(raw[6]))
            temp.append(fin)
    print(m)
    print(temp[0])
    print('data type: %s' % type(dict))
    data.append(temp)

我得到的输出:

data type: <class 'dict'>
jan
('BANKNIFTY', datetime.datetime(2017, 1, 2, 9, 8), 18242.3, 18242.3, 18242.3, 18242.3)
data type: <class 'type'>
Traceback (most recent call last):
  File "backtest.py", line 28, in <module>
    data.append(temp)
AttributeError: '_io.TextIOWrapper' object has no attribute 'append'

0 个答案:

没有答案