type mismatch - Python merge items from two rows -
it's ok if have regular format file, this:
period end date 09/30/ 06/30/ 03/31/ 12/31/ 09/30/
2012 2012 2012 2011 2011
then can merge these dates zip or print "%s%s" % (row_1[j], row_2[j])
but have irregular input this:
period end date 09/30/2012 06/30/ 03/31/2011 12/31/ 09/30/2012
2011 2010
or this:
period end date 09/30/ 06/30/ 03/31/2011 12/31/2011 09/30/2012
2012 2011
so final date merge of row_1 , row2 column, problem how dose python know column is. how should approach this? appreciate much!
there lots of ways it, each 1 generalizing different class of inputs-like-this. how about:
def dates_from_two(line1, line2): line2 = line2.split() word in line1.split(): wsplit = word.split('/') if len(wsplit) == 3: yield word if wsplit[-1] else (word + line2.pop(0)) open("period.txt") fp: lines = fp.readlines() i, line in enumerate(lines): if line.startswith("period end date"): next_line = lines[i+1] if i+1 < len(lines) else '' dates = list(dates_from_two(line, next_line)) print dates
which gives (for 3 cases):
['09/30/2012', '06/30/2012', '03/31/2012', '12/31/2011', '09/30/2011'] ['09/30/2012', '06/30/2011', '03/31/2011', '12/31/2010', '09/30/2012'] ['09/30/2012', '06/30/2011', '03/31/2011', '12/31/2011', '09/30/2012']
basically, above reads lines memory (not necessary, simpler if file isn't big) , loops on them, looking lines start "period end date". if finds one, sends line , next dates_from_two.
dates_from_two loops on each word in line1 , tries split /
. if produces 3 parts, we'll assume it's date. if so, yield date if last part of date nonempty, otherwise yield sum of word plus first term of line2 (which pop.) if never need line2, never use it. note line2.pop(0)
means "take first element of line2 , delete it".
Comments
Post a Comment