0

使用 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

我希望两行输出相同,以便像 row[1] == prevrow[1] 这样的比较会将 12392.0 与前一行中的第二个项目 (12392.0) 进行比较,而不是行中的第二个字符 ( 0)。所以,我的问题是 prevrow 已经变成了一个字符列表,当我尝试以我可以在行中索引项目的方式索引 prevrow 中的项目时,我似乎得到了列表中字符的索引。

如何保存当前“行”并保留其上下文?

4

2 回答 2

0

您正在构建字符串line并将它们存储在prevrow; 而是存储一个列表:

line = row[:2] + [cumbid] + prevrow[3:5] + [str(int(float(prevrow[5])) - int(float(cumbid)))] + prevrow[6:15]
于 2012-07-08T15:38:45.087 回答
0

使用 tee 创建两个虚拟迭代器,增加一个,然后将它们压缩,以获得 (lastrow,currentrow) 元组的序列。(请参阅itertools 文档pairwise中的配方。)

然后你可以写:

for prevrow,row in pairwise(reader):
    ... etc ...
于 2012-07-08T15:25:55.543 回答