【发布时间】:2020-05-01 03:16:20
【问题描述】:
这个问题有点涉及设置,但请耐心等待!
(将以下块复制并粘贴到编辑器中效果很好)
我正在使用 smartcsv 从金融网站的 csv 文件中加载我的数据。 每行都作为一个项目存储在列表中。
data = clevercsv.wrappers.read_csv(in_file_name)
在一些帐户信息行之后,股票数据开始:
stock_data = data[8:]
我希望删除数据:市场、贷款价值 - 一直到 - 日高(包括 0
并保留符号、描述 -> 持仓百分比(含)、52 周低位、52 周高位
每只股票的相关行上都有与之关联的数据。 删除此数据的任何最佳做法?我一直在尝试,但似乎有逻辑错误。
截至日期,2020-04-29 18:44:29
账户,道明自管投资 - 哈哈哈
现金,123.12
投资,1234.12
总值,12345.12
保证金,123456.12,
,
符号、市场、描述、数量、平均成本、价格、账面成本、市场价值、未实现的美元、未实现的百分比、头寸的百分比、贷款价值、今日变动美元、今日变动百分比、出价、出价手数、要价、询价手数、成交量、日低、日高、52 周低点、52 周高点
AFL,US,"AFLAC INC",500,43.79,39.23,21895.79,19615.00,-2280.79,-10.42,7.26,,1.4399986,3.81,39.19,1,40.2,1,3001288,38.31,39.48,23.07 ,57.18
AKTS,US,"AKOUSTIS TECHNOLOGIES INC",2500,5.04,8.94,12609.87,22350.00,9740.13,77.24,8.27,,0.35999966,4.20,8.68,1,9.2,10,1161566,8.65,9.25,3.76, 9.25
到目前为止,这是我的代码:
import clevercsv
data = clevercsv.wrappers.read_csv(in_file_name)
# store the earlier lines for later use, all rows 8 and later are stock data
cash = data[2]
investments = data[3]
tot_value = data[4]
margin = data[5]
full_header = data[7]
stock_data = data[8:]
new_header = []
new_stock_data = []
# I have found the index positions I wish to save, append their data to the new_ lists:
for i in range(len(full_header)):
if i == 0:
new_header.append(full_header[i])
if (i >= 2 and i <= 10):
new_header.append(full_header[i])
if i == 21:
new_header.append(full_header[i])
if i == 22:
new_header.append(full_header[i])
# I have found the index positions I wish to save, append their data to the new_ lists:
for i in range(len(stock_data)):
if i == 0:
new_stock_data.append(stock_data[i])
if (i >= 2 and i <= 10):
new_stock_data.append(stock_data[i])
if i == 21:
new_stock_data.append(stock_data[i])
if i == 22:
new_stock_data.append(stock_data[i])
with open(os.path.join(folder_path,out_file_name),'w') as out_file:
writer = clevercsv.writer(out_file)
writer.writerow(cash)
writer.writerow(investments)
writer.writerow(tot_value)
writer.writerow(margin)
writer.writerow(new_header)
for row in new_stock_data:
writer.writerow(row)
如果这太复杂了,我理解,如果有人有更好的库可以使用,或者有更好的方法来使用 csv 库,那么它本身就会有很多帮助。
【问题讨论】:
-
这看起来是 Pandas 的工作:pandas.pydata.org