Search through csv from a specific line down

This is the next step: this question

The code I use is:

import csv file1reader = csv.reader(open("file1.csv"), delimiter=",") file2reader = csv.reader(open("file2.csv"), delimiter=",") header1 = file1reader.next() #header header2 = file2reader.next() #header for Code, DTime in file1reader: for id_, D, Sym, DateTime, Bid, Ask in file2reader: if DateTime.startswith(DTime): # found it print id_, Code, DateTime, Bid, Ask # output data break # break and continue where we left next time 

Example result:

 1375023013 1 2010-12-26 17:01:01 1.311200 1.311500 1375023592 2 2010-12-26 17:07:16 1.311700 1.312000 1375024176 2 2010-12-26 17:15:04 1.311300 1.311600 

With each row of these results I want to find the id_ value in the file2.csv and from this row down, the Bid search column for the value> = Bid ​​+ 0.0010 OR <= Bid-0.0015, Basically, starting from this record, what is the price of first place? Bid + 0.0010 or Bid-0.0015? I need to use> = and <=, since prices are not continuous. Once this record is found, I want to add the original result by adding a column for the found DateTime and Bid.

Expected Result:

 id_ Code DateTime Bid Ask NextDateTime NextBid 1375023013 1 2010-12-26 17:01:01 1.311200 1.311500 2010-12-26 17:03:02 1.312200 1375023592 2 2010-12-26 17:07:16 1.311700 1.312000 2010-12-26 17:09:03 1.311100 1375024176 2 2010-12-26 17:15:04 1.311300 1.311600 2010-12-26 17:20:02 1.312400 

Thanks again

+2
source share
1 answer

To "search through csv from a specific line down," try using itertools.dropwhile .

To "add the original result by adding a column", you will need to rewrite the file with the new data. fileinput module makes this easy to do when inplace is set to true).

+3
source

Source: https://habr.com/ru/post/1399745/


All Articles