how to sort text file alphabetically with python?

  python, python-3.x

I have a text file that contains English words translated into Arabic that I want to arrange alphabetically,

text file :

entrenched = ترسخ

hypotenuse =وتر

conquered = التغلب

tempted = يغري

intentional = متعمد

ps: some words contains more the one like this :

indistinguishable = لا يمكن تمييزه 
spot the subtle = بقعة الخفية

I want to check just the first char of every lines and make the sort , I tried but it didn’t work :

def sorting(filename):
infile = open(filename,encoding="utf8")
words = [] 
 for line in infile:
 temp = line.split()
  for i in temp:
  words.append(i)
 infile.close()
 words.sort()
 outfile = open("result.txt", "w",encoding="utf8")
 for i in words:
outfile.writelines(i)
outfile.writelines("n")
 outfile.close()
 sorting("words.txt")

Source: Python-3x Questions

LEAVE A COMMENT