And that is because your not owning the output -- create a function that represents a line -- within this function map out the elements of that line such as the following (note the following is pseudo code you will need to translate that into working code):
#These would be the Headers ColIdx = 0.1 For Each ItmId in HdrItem: RowElements[ColIdx] = HdrItem[ItmId] ColIdx += 0.1 MaxCol = ColIdx -= 0.1 ColIdx = 0.1 RowIdx = 1 For Each RowId in DataFeed: RowElements[(RowId+ColIdx)] = DataFeed[RowId] ColIdx += 0.1 if ColIdx > MaxCol: RowIdx += 1 ColIdx = 0.1 # This gets the maximum width of each column for each RowCol in RowElements: Col = ConvertRowColtoCol(RowCol) if ColWidth[Col] < len(RowElements[RowCol]): ColWidth[Col] = len(RowElements[RowCol]) # Now using these 2 Dictionaries you build a string output from them such that RowIdx = 0 for each RowCol in RowElements: Col = ConvertRowColtoCol(RowCol) ColWid = ColWidth[Col] Row += CenterCol(RowElements[RowCol],ColWid) if Col == MaxCols: RowOut[RowIdx] = Row Row = '' RowIdx += 1 for each Row in RowOut: OutputRowToFile(Row)