Python を使用して、次のスクリプトを作成し、csv.reader 内の行にインデックスを付けて、前の行の関連する値と比較します。
source = open('sourcefile')
reader = csv.reader(source)
with open('targetfile','w') as f:
for row in reader:
if row[1] == "NA" and row[2] == "NA":
prevrow = row
line = row
writer = csv.writer(f)
writer.writerow(line)
else:
while row[1] == prevrow[1]:
if row[1] == prevrow[3]:
cumbid = cumbid + row[2]
line = row[0] + "," + row[1] + "," + cumbid + "," + prevrow[3] + "," + prevrow[4] + "," + str(int(float(prevrow[5]))-int(float(cumbid))) + "," + prevrow[6] + "," + prevrow[7] + "," + prevrow[8] + "," + prevrow[9] + "," + prevrow[10] + "," + prevrow[11] + "," + prevrow[12] + "," + prevrow[13] + "," + prevrow[14] + "," + ","
prevrow = line
elif row[1] == prevrow[4]:
cumask = cumask + row[2]
line = row[0] + "," + row[1] + "," + cumask + "," + prevrow[3] + "," + prevrow[4] + "," + prevrow[5] + "," + str(int(float(prevrow[6]))-int(float(cumask))) + "," + prevrow[7] + "," + prevrow[8] + "," + prevrow[9] + "," + prevrow[10] + "," + prevrow[11] + "," + prevrow[12] + "," + prevrow[13] + "," + prevrow[14] + "," + ","
prevrow = line
else:
if row[1] == prevrow[3] and row[2] < prevrow[5]:
cumbid = row[2]
line = row[0] + "," + row[1] + "," + row[2] + "," + prevrow[3] + "," + prevrow[4] + "," + str(int(float(prevrow[5]))-int(float(row[2]))) + "," + prevrow[6] + "," + prevrow[7] + "," + prevrow[8] + "," + prevrow[9] + "," + prevrow[10] + "," + prevrow[11] + "," + prevrow[12] + "," + prevrow[13] + "," + prevrow[14] + "," + ","
prevrow = line
print prevrow
print prevrow[0]+ " " + prevrow[1]+ " " + prevrow[2]+ " " + prevrow[3]+ " " + prevrow[4]+ " " + prevrow[5]+ " " + prevrow[6]
writer = csv.writer(f)
writer.writerow(line.split())
elif row[1] == prevrow[4] and row[2] < prevrow[6]:
cumask = row[2]
line = row[0] + "," + row[1] + "," + row[2] + "," + prevrow[3] + "," + prevrow[4] + "," + prevrow[5] + "," + str(int(float(prevrow[6]))-int(float(row[2]))) + "," + prevrow[7] + "," + prevrow[8] + "," + prevrow[9] + "," + prevrow[10] + "," + prevrow[11] + "," + prevrow[12] + "," + prevrow[13] + "," + prevrow[14] + "," + ","
prevrow = line
print prevrow
print prevrow[0]+ " " + prevrow[1]+ " " + prevrow[2]+ " " + prevrow[3]+ " " + prevrow[4]+ " " + prevrow[5]+ " " + prevrow[6]
writer = csv.writer(f)
writer.writerow(line.split())
elif row[1] == prevrow[3] and row[2] == prevrow[5] :
line = row[0] + "," + row[1] + "," + row[2] + "," + prevrow[7] + "," + prevrow[4] + "," + prevrow[8] + "," + prevrow[6] + "," + prevrow[11] + "," + prevrow[12] + "," + prevrow[9] + "," + prevrow[10] + "," + "," + "," + "," + "," + "," + "," + ","
prevrow = line
print prevrow
print prevrow[0]+ " " + prevrow[1]+ " " + prevrow[2]+ " " + prevrow[3]+ " " + prevrow[4]+ " " + prevrow[5]+ " " + prevrow[6]
writer = csv.writer(f)
writer.writerow(line.split())
elif row[1] == prevrow[4] and row[2] == prevrow[6]: #the trade was on the offer and took it all out--we have to refer to the 2nd level of the book
line = row[0] + "," + row[1] + "," + row[2] + "," + prevrow[3] + "," + prevrow[9] + "," + prevrow[5] + "," + prevrow[10] + "," + prevrow[7] + "," + prevrow[8] + "," + prevrow[13] + "," + prevrow[14] + "," + "," + "," + "," + "," + "," + "," + ","
prevrow = line
print prevrow
print prevrow[0]+ " " + prevrow[1]+ " " + prevrow[2]+ " " + prevrow[3]+ " " + prevrow[4]+ " " + prevrow[5]+ " " + prevrow[6]
writer = csv.writer(f)
writer.writerow(line.split())
elif row[1] == prevrow[1] and row[1] == prevrow[3] : #trade was on the bid, and the prev row was also a trade--decrement bid size
line = row[0] + "," + row[1] + "," + row[2] + "," + prevrow[3] + "," + prevrow[4] + "," + str(int(float(prevrow[5]))-int(float(row[2]))) + "," + prevrow[6] + "," + prevrow[7] + "," + prevrow[8] + "," + prevrow[9] + "," + prevrow[10] + "," + prevrow[11] + "," + prevrow[12] + "," + prevrow[13] + "," + prevrow[14] + "," + ","
prevrow = line
writer = csv.writer(f)
writer.writerow(line.split())
elif row[1] == prevrow[1] and row[1] == prevrow[4] : #trade was on the offer, and the prev row was also a trade--decrement ask size
line = row[0] + "," + row[1] + "," + row[2] + "," + prevrow[3] + "," + prevrow[4] + "," + prevrow[5] + "," + str(int(float(prevrow[6]))-int(float(row[2]))) + "," + prevrow[7] + "," + prevrow[8] + "," + prevrow[9] + "," + prevrow[10] + "," + prevrow[11] + "," + prevrow[12] + "," + prevrow[13] + "," + prevrow[14] + "," + ","
prevrow = line
writer = csv.writer(f)
writer.writerow(line.split())
ただし、これらの後続の行は次を生成します。
コード
print prevrow
print prevrow[0]+ " " + prevrow[1]+ " " + prevrow[2]+ " " + prevrow[3]+ " " + prevrow[4]+ " " + prevrow[5]+ " " + prevrow[6]
出力
20100628000120702,12392.0,1.0,12392.0,12393.0,5.0
2 0 1 0 0 6 2
行の 2 番目の文字 ( 0)。したがって、私の問題は、prevrow が文字のリストになっていることです。行の項目にインデックスを付けることができる方法で prevrow の項目にインデックスを付けようとすると、代わりにリスト内の文字のインデックスを取得するようです。
現在の「行」を保存してそのコンテキストを保持するにはどうすればよいですか?