3
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.

  • This is odd, as soon as some of these persian chars are added to a list, its elements still have the right index but the list is represented on a reverse order... In your exemple, result[0][0] will output 'پژو 206', but when you print the list, this item appears as if it were the last one – olinox14 Mar 03 '21 at 15:36
  • Are you sure the file is written and you get a new file? If I add the encoding in `open('oneshot.csv', mode='w', encoding='UTF-8')` and drop the extra list wrapper in `writer.writerow(item)` everything looks fine. – Matthias Mar 03 '21 at 15:45
  • @Matthias Even if I get rid of the CSV thing and only instantiate the `results` list, when I try to print `results[0][0]`, I get the 'پژو 206' item. However, if I do `print(results[0])`, this very same item appear in last position... I'm very curious about it. I tried to take a look at the [cpython listobject source](https://github.com/python/cpython/blob/master/Objects/listobject.c), but could'nt find anything that could explain such behavior – olinox14 Mar 03 '21 at 15:48
  • Could this be it something about the right to left reading implied by persian? – olinox14 Mar 03 '21 at 15:51
  • @olinox14 yes, I think the same. this issue would be best addressed by a person who uses Persian keyboard layout, or atleast has worked with Persian language projects. – Rishabh Kumar Mar 03 '21 at 15:58
  • 1
    Maybe something to find here: https://stackoverflow.com/a/6679459/4279120 – olinox14 Mar 03 '21 at 15:59

2 Answers2

3

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.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Thanks, I didn't know a thing about the existence of the [unicode left-to-right override char](https://www.fileformat.info/info/unicode/char/202d/index.htm), using this is the best solution in my opinion – olinox14 Mar 04 '21 at 09:51
  • @MarkTolonen Question with some samples edited please check. so that it is not just a question of inverting the data. Thanks for introducing left to right override:) – Amirhossein Mar 06 '21 at 07:38
2

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:

Rishabh Kumar
  • 2,342
  • 3
  • 13
  • 23