SS1=[(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)]
SS2=[(1, 2, 3, 4), (1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)]
SS3=[(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
I'm Writing my list into a file using the below codes...But when i write again it's adding to the last line of the file.
but what i want is....it should append horizontally not vertically. I mean must go like table and fields and records.
so, for the above ss1
the line of records is going to be 6 and for every save should add the values in parallel as table fields and records manner
ss1 ss2 ss3 1, 2, 3, 4, 5 1, 2, 3, 4 1, 2, 3
def write_to_txt(a, file_name, delimiter=','):
with open(file_name, 'a') as f:
for k in SS1:
fmt = '%s' % delimiter
f.write(fmt.join(map(str, k)) + '\n')
not only writing and help me to read it back to same number of lists based on it's number columns.
def read_from_txt(file_name):
with open(file_name, 'r') as f:
data = [tuple(map(int, k.split(','))) for k in f.read().splitlines()]
return data
#write_to_txt(SS1, 'ABC.txt')
#data = read_from_txt('ABC.txt')
This is a good first aproach:
SS1=[(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)]
SS2=[(1, 2, 3, 4), (1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)]
SS3=[(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
ALL = [SS1,SS2,SS3]
def write_to_txt(a, file_name, delimiter=','):
with open(file_name, 'a') as f:
for i, ss in enumerate(SS1):
if i != 0:
f.write("\n")
for ss in ALL:
fmt = '%s' % delimiter
f.write(fmt.join(map(str, ss[i])) + ",")
write_to_txt("a","lines.txt")