I have the following code to join the file and trying to sort the file in ascending order before writing the result into the final .txt file. But it is showing string object has no attribute.
temp_list = ''
with open('temp.txt') as f:
out = [x for x in f.read().split("\n") if x.strip() != ""]
for line1, line2 in list(zip(out, out[1:]))[::2]:
line1 = line1 + ';'
line2 = line2.split(' ')
line = ''
for x in range(1,len(line2)):
line = line + line2[x] + ' '
line = line[:-1] + '.'
temp_list += " ".join([line1, line]) + '\n'
temp_list.sort()
with open('new.txt' , 'w') as file:
file.write(temp_list)
temp.txt
line1 Ron likes apple; mango
line5 Ana likes lyche; apple
line21 Tyson likes football; VolleyBall
line6 Mike likes singing; dancing
line245 Stephen likes playing; sleeping
line95 Rose likes dancing; singing
The order of the lines should be like
line1
line5
line6
line21
line95
line245
but not like
line1
line21
line245
line5
line6
line95
Any help would be appreciated ..Thank you