【问题标题】:How to sort urdu words in python or bash如何在python或bash中对乌尔都语单词进行排序
【发布时间】:2015-11-13 13:50:14
【问题描述】:

我有一个包含乌尔都语单词的文本文件。我必须删除它的重复项。为此,需要对这些单词进行排序。在英语中,这不是问题,但是当我遵循相同的乌尔都语时,它就成了一个问题(错误)。对于测试用例,假设我的文本文件包含流动的单词(为简单起见,每行一个单词)

جنگ
بندی
 اس
کروانا
سات
 اس
سات

以下是代码和错误。

[example@localhost compare]$ ./get_urdu_words.py |sort

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

这里的 get_urdu_words.py 是一个从 urdu/English 混合文件中提取 urdu 单词的脚本,sort 是 bash 命令。

这是get_urdu_words.py的代码

import re
test=u"جنگ بندی  اس کروانا سات  اس سات"

token=test.split(' ')
for w in token:
 status=re.search(ur'[\u0600-\u06ff]+',w)
 if status:
  print w

这个问题只针对乌尔都语

【问题讨论】:

  • 有什么错误?
  • 你在尝试什么代码?常规的sort 函数在乌尔都语上不起作用吗?
  • 重新更新:错误在你的get_urdu_words.py。与sort 或 bash 无关。
  • 请出示get_urdu_words.py文件
  • 其实这和stackoverflow.com/questions/492483/…是一模一样的问题。

标签: python


【解决方案1】:

稍作修改即可解决您的问题。试试这个

import re
test=u"جنگ بندی  اس کروانا سات  اس سات"

token=test.split(' ')
for w in token:
 status=re.search(ur'[\u0600-\u06ff]+',w)
 if status:
  print w.encode('utf-8')

在此之后运行此命令

[example@localhost compare]$ ./get_urdu_words.py |sort

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多