【发布时间】:2020-12-30 08:57:54
【问题描述】:
嘿,我有一列'close'中的浮点数需要转换为累积百分比并将其存储在'cum_p'中。
我得到了这个脚本来帮助我,但我搞砸了一两步:
import os
import sys
import csv
def adjust_cryptors_file(source, /, values, close):
with open(source) as f:
data = [row for row in csv.reader(f)][1:]
agg_data = []
ix = 0
total = 0
while ix < len(data):
# value to add to the running total
row = data[ix]
# Column 20 / Index 19
v = float(row[values])
total += v
# percentage of running total
p = (total / 3797.14) * 100
closed = row[close]
# add to new list of data
agg_data.append([v, p, closed])
# increment index counter
ix += 1
agg_data.insert(0, ['timestamp', 'close', 'cum_p'])
parent = os.path.dirname(source)
dest = os.path.join(parent, 'modified.data')
with open('modified.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(agg_data)
print(f"Your new modified data file: {dest}")
if __name__ == '__main__':
# Enter your CSV file here
source = 'BTCUSDT-1d-data.csv'
column_of_timestamp = 1
column_of_close = 5
adjust_cryptors_file(source, values=column_of_timestamp, close=column_of_close)
我非常感谢一些帮助或指点 :) 第一个值是 0%,第二个值是 'close' 中第一个和第二个值的百分比差异。
希望任何人都可以帮助我。
【问题讨论】:
-
您愿意分享您正在使用的 .csv 文件吗?我想帮助购买希望确保格式符合您正在使用的格式,这样我就不会得到可能不兼容的解决方案。