代码之家  ›  专栏  ›  技术社区  ›  Naila Akbar

乌尔都语字符串看起来一样,但在比较中发现不平等的蟒蛇3

  •  1
  • Naila Akbar  · 技术社区  · 6 年前

    enter image description here

    我还有另一个文本文件,上面有乌尔都语字符串(目前只有一个单词像这样,完全相同)

    enter image description here

    //正在读取字符串的文本文件。。。

    fileToRead = codecs.open('string.txt', mode, encoding=encoding)
    fileData = fileToRead.read()
    lstFileData = fileData.split('\n')
    
    
    wordListToRead = codecs.open('words.txt', mode, encoding=encoding)
    wordData = wordListToRead.read()
    lstWords = wordData.split('\n')
    

    我只是像这样遍历列表;

    for string in lstFileData:
        if string in lstWords:
            // do further work
    

    2 回复  |  直到 6 年前
        1
  •  1
  •   golddove    6 年前

    刚刚在python3中试用过,似乎对我很有用:

    lstWords = ['a', 'فلسفے', 'b']
    string = 'فلسفے'
    if string in lstWords:
        print("yes")
    

    编辑:同样,刚刚用文件IO测试了更新后的代码,效果很好(我没有指定编码)。以下是it工作的链接: https://trinket.io/python3/3890d8b261

        2
  •  0
  •   Naila Akbar    6 年前

    虽然听起来很有趣,但问题是 file encoding type . 我在简单的记事本中打开文件进行一些更改并保存了它。它把我的文件从 utf-8 utf-8 BOM . 我的代码没用。一旦我用utf-8在notepad++中创建了新文件,同样的代码就开始正常工作了(因为问题不在代码中,而是在文件编码中)

    推荐文章