I am trying to save my contents in the textfile in this format
|Name|Description|
|QY|Hello|
But for now mine is returning me as this output:
|2014 1Q 2014 2Q 2014 3Q 2014 4Q|
|96,368.3 96,808.3 97,382 99,530.5|
Code:
def write_to_txt(header,data):
with open('test.txt','a+') as f:
f.write("|{}|\n".format('\t'.join(str(field)
for field in header))) # write header
f.write("|{}|\n".format('\t'.join(str(field)
for field in data))) # write items
Any idea how to change my code so that I would have the ideal output?
This is happenning because you are joining the fields inside data iterable using \t
, use |
in the join as well for your requirement. Example -
def write_to_txt(header,data):
with open('test.txt','a+') as f:
f.write("|{}|\n".format('|'.join(str(field)
for field in header))) # write header
f.write("|{}|\n".format('|'.join(str(field)
for field in data)))