【发布时间】: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