如何解决为什么在写入 csv 文件以及添加到列表时,数字字符串和字符串文本波斯语的顺序会发生变化?
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,price):
writer.writerow([name,price])
输出:
پژو 206,1382,250000,مشکی,تهران,585000000
ام وی ام x55,1399,خاکستری,178000000
رنو ساندرو,1394,سفید,میدان آرژانتین,332000000
但我期望作为输出示例的是:
تهران,پژو 206,price(585000000)
根据我给出的例子,需要注意的是,这不仅仅是数据反演的问题。
这是由于波斯语与英语数字不兼容造成的混乱吗?
解决此问题的一种方法是通过 num2words 库将数字转换为单词。之后使用words2num库分析数据,我们可以将数字转换为文本到数字,这似乎是一项额外而乏味的任务。如果你有更快的解决方案,我很乐意帮助我:)
预先感谢您的指导。
解决方法
这是混合从左到右和从右到左语言的结果,它混淆了 IDE/浏览器显示。考虑以下内容,它提供了一个 Unicode 代码点来覆盖由打印波斯语引起的从右到左的默认值:
for things in nlp.pipe(example1):
print(things)
#First iteration prints things
a is something
b is other thing
c is new thing
d is extra
for things in nlp.pipe(example1):
print(things)
#Second iteration prints things again!
a is something
b is other thing
c is new thing
d is extra
输出:
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',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)
请注意,不同的浏览器和 IDE 可能会根据它们对从左到右语言的支持程度不同而显示不同。例如,我的 IDE 向后打印 LTR 覆盖行的第一个括号,但我在查看此 StackOverflow 页面的 Chrome 浏览器中看不到这一点。
,列表顺序没有改变,只是反过来显示。
因此,由于要从右到左阅读波斯语,系统会识别正在使用的语言并显示要反过来阅读的列表
测试:
result=[]
if True:
name = 'پژو 206'
model = '1382'
func = '250000'
color = 'مشکی'
city = 'تهران'
price = '585000000'
result.append([name,price])
print(result,'\n')
print(result[0],'\n')
for i in range(len(*result)):
print(i,':',result[0][i])
输出:
[['پژو 206','1382','250000','مشکی','تهران','585000000']]
['پژو 206','585000000']
0 : پژو 206
1 : 1382
2 : 250000
3 : مشکی
4 : تهران
5 : 585000000
如果系统不将输出视为波斯语(作为语言),则实际输出可能仍以正常方式显示。
例如。在我的控制台中,输出加载如下:
编辑 基于 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,price])
#result = [['پژو 206','585000000']]
with open('temp.csv','w') as op:
op.write(','.join(*result))
控制台输出(xfce4-terminal v0.8.7.4):
LibreOffice Calc v6.4 上的输出:
就像我之前说的,它也取决于观众。我的控制台超出了所有从左到右或从右到左的东西。 它首先显示索引 0 处的内容。
最终编辑:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。