【问题标题】:identify non Latin Charset text Ph识别非拉丁字符集文本 Ph
【发布时间】:2019-02-18 06:41:21
【问题描述】:

从一个巨大的文本文件中,需要能够识别包含非拉丁字符(\w 加特殊字符)的行,技术上我应该排除拉丁以外的其他字母。输出存储在日志文件中以供进一步处理。 我对re 的尝试没有成功,您是否看到了一种识别和散发包含非拉丁字符的行的聪明方法。

import pandas as pd
import re
pattern = '^\w+$'
regex = re.compile(pattern)
filename = "C:\\ImportTool\\import\\file.csv"
with open(filename, 'r', encoding='utf8', errors='ignore') as inputfile, \
     open(filename + '.clean', 'w', encoding="utf8") as outputfile, \
     open(filename + '.special', 'w', encoding="utf8") as outputfile_log:
        for index, line in enumerate(inputfile):
            #print(index, (line_aux[:]))
            if  (regex.search(line) == None):
                outputfile.writelines(line)
            else:
                outputfile_log.writelines(line)

I.e 下面的行应该被排除,因为内容是希伯来语

"100";"xxxxxxxxx";"00002";"ZM";"B";"";"";"B";"R";"R";"X";"RR";"I02";"OxxH";"20161107";"ybatuca";"זמניים מחלקת תיפעול חיפה";"";"";"IL01";"";"";"";"";"1000.000 "

【问题讨论】:

  • 您的搜索问题在于\w 仅匹配拉丁语语言中使用的一小部分字符——单词字符。是要排除所有包含非拉丁字母字符的行,还是要排除所有不包含拉丁字母字符的行?
  • 我想排除所有包含非拉丁字母字符的行
  • 您在寻找\pL 字符吗???

标签: regex python-3.x pandas


【解决方案1】:

Python 包含 unicodedata 模块用于类似的事情:

import unicodedata
...

for index, line in enumerate(inputfile):
    if any(unicodedata.category(ch).startswith("L") and not unicodedata.name(ch).startswith("LATIN")):
        ...  # contains non-latin alphabetic chars
    else:
        ...  # doesn't contain non-latin alphabetic chars

【讨论】:

    猜你喜欢
    • 2015-06-26
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    相关资源
    最近更新 更多