Search code examples
pythonlistcsvunicoderight-to-left

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?


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.


Solution

  • 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: