新手学python,正在研究股票行情。
1)当前有一个excel,里面包含三千余只曾经发行过定增的股票,已经知道股票代码,定增发行日和定增解禁日。
2)另外有一个文件夹,里面包含3800个csv文件,文件名为股票代码,包含股票上市交易数据,包括收盘价、市值、涨跌幅之类的。
需求,如何将excel文件中特定的交易日股价从对应的csv文件中提取出来,并在一个excel中新增3列,2列为对应日期的股价,第三列去两个日期的区间涨跌幅。
目前个人代码能提出股价信息,但是写入excel中出现问题,并且excel无法正常打开。 求各位大佬指教
path = 'D:/python and model/allstockprice2/'
csvfile = path + stockcode + ".CSV"
print(path, csvfile)
with open(csvfile,'r') as stockfile:
reader = csv.DictReader(stockfile)
book = xlwt.Workbook(encoding='utf-8') #创建Workbook,相当于创建Excel
# 创建sheet,Sheet1为表的名字
sheet1 = book.add_sheet(u'gujia')
sheet1.write(0,1,'股票代码')
sheet1.write(0,2,'发行日')
sheet1.write(0,3,'发行日股价')
sheet1.write(0,4,'A股流通市值')
sheet1.write(0,5,'总市值')
sheet1.write(0,6,'解禁日')
sheet1.write(0,7,'解禁日股价')
for row in reader:
if row['日期']==faxingri:
#将结果写入excel row['开盘价(元)'],row['收盘价(元)']
sheet1.write(1,1,stockcode)
sheet1.write(1,2,row['日期'])
sheet1.write(1,3,row['收盘价(元)'])
sheet1.write(1,4,row['A股流通市值(元)'])
sheet1.write(1,5,row['总市值(元)'])
if row['日期']==jiejinri:
#将结果写入excel row['开盘价(元)'],row['收盘价(元)']
sheet1.write(1,6,row['日期'])
sheet1.write(1,7,row['收盘价(元)'])
book.save('D:/python and model/定增/定向增发发行个股资料2.xls')