Why does the order of string of numbers and string text (In Persian language) change when writing in the csv file as well as adding to the list?

Viewed 103
import csv
with open('cars_data.csv', mode='w') as csv_file:
    writer = csv.writer(csv_file, delimiter=',', quoting=csv.QUOTE_MINIMAL)
    name = ['پژو 206', 'ام وی ام x55', 'رنو ساندرو']
    model = ['1382', '1399', '1394']
    function = ['250000', '0', '0']
    color = ['مشکی', 'خاکستری', 'سفید']
    city = ['تهران', 'تهران', 'میدان آرژانتین']
    price = ['585000000', '178000000', '332000000']
    for name, model, function, color, city, price in zip(name, model, function, color, city, price):
        writer.writerow([name, model, function, color, city, price])

output:

پژو 206,1382,250000,مشکی,تهران,585000000
ام وی ام x55,1399,0,خاکستری,تهران,178000000
رنو ساندرو,1394,0,سفید,میدان آرژانتین,332000000

But what I expect as an example of the output is:

تهران ,مشکی ,250000 ,1382 ,پژو 206, price(585000000)

According to the examples I have given, it should be noted that it is not just a question of inverting data.

Is this confusion due to the Persian language not being compatible with English numbers?

One way to solve this problem is to convert numbers to words by the num2words library. And later to analyze the data using the words2num library, we can convert the numbers into text to numbers, which seems like an extra and tedious task. If you have a faster solution, I will be happy to help me:)

Thank you in advance for your guidance.

2 Answers

It's a consequence of mixing left-to-right and right-to-left languages, and it confuses the IDE/browser display. Consider the following, which provides a Unicode codepoint to override the right-to-left default caused by printing Persian:

import csv

ltr = '\N{LEFT-TO-RIGHT OVERRIDE}'

with open('cars_data.csv', 'w', encoding='utf8', newline='') as csv_file:
    writer = csv.writer(csv_file)
    name = 'پژو 206'
    model = '1382'
    func = '250000'
    color = 'مشکی'
    city = 'تهران'
    price = '585000000'
    writer.writerow([name, model, func, color, city, price])

with open('cars_data.csv','r',encoding='utf8',newline='') as csv_file:
    reader = csv.reader(csv_file)
    for row in reader:
        print(row)      # Row is printed backward
        print(ltr,row)  # With left-to-right override
        for col in row: # Columns print in the correct order
            print(col)

Output:

['پژو 206', '1382', '250000', 'مشکی', 'تهران', '585000000']
‭ ['پژو 206', '1382', '250000', 'مشکی', 'تهران', '585000000']
پژو 206
1382
250000
مشکی
تهران
585000000

Note that different browsers and IDEs may display this differently depending on how well they support left-to-right languages. My IDE prints the first bracket of the LTR override line backward, for example, but I don't see that in Chrome browser viewing this StackOverflow page.

The list sequence is not changed, its just displayed the other way round.

So as Persian language is meant to be read right to left, the system identifies the language being used and displays the list to be read the other way round

Testing:

result=[]
if True:
    name = 'پژو 206'
    model = '1382'
    func = '250000'
    color = 'مشکی'
    city = 'تهران'
    price = '585000000'
    result.append([name, model, func, color, city, price])

print(result,'\n')

print(result[0],'\n')

for i in range(len(*result)):
    print(i,':',result[0][i])

Output:

[['پژو 206', '1382', '250000', 'مشکی', 'تهران', '585000000']] 

['پژو 206', '1382', '250000', 'مشکی', 'تهران', '585000000'] 

0 : پژو 206
1 : 1382
2 : 250000
3 : مشکی
4 : تهران
5 : 585000000

The actual output maybe still displayed the normal way if the system doesn't treat the output to be Persian (as language).

eg. in my console, the output loads like:

enter image description here

Edit Based on new example provided by OP:

import csv
result = []
with open('cars_data.csv', mode='w') as csv_file:
    writer = csv.writer(csv_file, delimiter=',', quoting=csv.QUOTE_MINIMAL)
    name = 'ام وی ام x55'
    model = '1399'
    func = '0'
    color = 'خاکستری'
    city = 'تهران'
    price = '178000000'
    result.append([name, model, func, color, city, price])
    #result = [['پژو 206', '1382', '250000', 'مشکی', 'تهران', '585000000']]
    with open('temp.csv','w') as op:
        op.write(','.join(*result))

Output on console (xfce4-terminal v0.8.7.4):

Output on LibreOffice Calc v6.4:


Like I said earlier, its dependent upon the viewer too. My console is beyond all Left-to-Right or Right-to-left stuff. It displays stuff first which is at index 0.

Final Edit:

Related