'Failure to follow the order of the words when inserting them in the file in python

we have a list with arabic words for exmple

list[0]="هذه"
list[1]="هذا"
list[2]="ذلک"

and we need write each item of that list to a text file in index order and separate them by | character example of written file is

list[0]|list[1]|list[2]

and in example of question output must be

The first word to be written in the file : هذه

The second word to be written in the file :هذا

And the third word to be written in the file :ذلک

but output of file is similar below

هذه|هذا|ذلک

the هذه word must be first and ذلک must be last but it is reverse

the code is

tf = open("temp file.txt", "w",encoding="utf8")
tf.write("هذه")
tf.write('|')
tf.write("هذا")
tf.write('|')
tf.write("ذلک")
tf.write('|')

how can i fix it ?



Solution 1:[1]

The question said use index order, but the original code used literals.

# Build the original list

list = [1,2,3]
list[0]="???"
list[1]="???"
list[2]="???"
print(f'Original list = {list}')
# Process the list
tf = open("temp file.txt", "w",encoding="utf8")
tf.write(list[2]) # use index not literals
tf.write('|')
tf.write(list[1])
tf.write('|')
tf.write(list[0])
tf.write('|')
tf.close()
result =open("temp file.txt",encoding="utf8")
print(f'{result.read()=}')
result.close()

Output

Original list = ['???', '???', '???']
result.read()='???|???|???|'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1