Python:如何在阅读器中保存一行并保留其上下文?

时间:2012-07-08 15:02:35

标签: python csv

使用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已成为一个字符列表,当我试图按照我可以在行中索引项目的方式对项目进行索引时,我似乎得到了列表中字符的索引。

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

2 个答案:

答案 0 :(得分:0)

使用tee创建两个虚拟迭代器,递增一个,然后压缩它们,以获得一系列(lastrow,currentrow)元组。 (请参阅itertools documentation中的pairwise食谱。)

然后你可以写:

for prevrow,row in pairwise(reader):
    ... etc ...

答案 1 :(得分:0)

您正在line中构建字符串并将其存储在prevrow中;改为存储一个列表:

line = row[:2] + [cumbid] + prevrow[3:5] + [str(int(float(prevrow[5])) - int(float(cumbid)))] + prevrow[6:15]