I want to align string and adjust spacing in a column with the correct custom format I want.
My code:
import time
seperator='|'+'-'*33+'|\n'
seperator2='|'+'='*33+'|\n'
end = '|'+'^'*33+'|'
t=['Tuesday','July','2022','03','06']
try:
with open('time.txt','r') as f:
content = f.readlines()
except:
with open('time.txt','w') as f:
f.write('pass')
with open('time.txt','r') as f:
content = f.readlines()
if content[0] != '_________________________________\n':
with open('time.txt','w') as f:
header= '_'*33+'\n'+\
'|Day |Month |Year |Hour |Minute |\n'
data = (f'|{t[0]} |{t[1]} |{t[2]}'
f'|{t[3]} |{t[4]} |\n')
f.write(header+seperator+data+end)
elif content[0] == '_________________________________\n':
with open('time.txt','r+') as f:
saved=f.readlines()[:-1]
f.seek(0)
data = (f'|{t[0]} |{t[1]} |{t[2]}'
f'|{t[3]} |{t[4]} |\n')
f.writelines(saved+[seperator2,data,end])
Output in the time.txt file(if it has been ran once):
_________________________________
|Day |Month |Year |Hour |Minute |
|---------------------------------|
|Tuesday |July |2022|03 |06 |
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|
Output(twice)(showing this to clarify that the data should be saved and re-printed):
_________________________________
|Day |Month |Year |Hour |Minute |
|---------------------------------|
|Tuesday |July |2022|03 |06 |
|=================================|
|Tuesday |July |2022|03 |06 |
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|
The output I want:
_____________________________________
|Day |Month |Year |Hour |Minute |
|--------|------|-----|-----|-------|
|Tuesday |July |2022 |03 |06 |
|========|======|=====|=====|=======|
|Tuesday |July |2022 |03 |06 |
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|
Any help would be appreciated, Thanks.