我正在尝试在包含多个段落的阿拉伯文本的文件中进行一些正则表达式匹配,但是我无法访问匹配对象的内容,我的代码如下所示:
import re
import pandas as pd
reg =r"^[\u0621-\u064A۰-۹0-9 ]+$"
dataset =pd.read_csv('test.txt',header=None,index_col=False)
for row in dataset.itertuples():
for pattern in enumerate(row,start=1):
pattern=row[1].split('\n')
print(pattern)
matches=re.finditer(reg,pattern,re.MULTILINE)
print(matches)
我正在使用嵌入在Python 3.6的Anaconda发行版中的spyder编辑器。
我尝试了re.match
函数,lambda x: x[1]
试图捕获内容,matches.group()
遍历匹配段,str(pattern)
和许多其他解决方案,但是没有一个他们按预期工作。
尽管我检查过我的文件是否以阿拉伯字母开头,但在某个时候我设法查看它是否首先找到了一个匹配项并给了我无!
另一方面,当将简单文本作为字符串直接传递给查找器时,我可以轻松完成任务!
import re
import pandas as pd
reg =r"^[\u0621-\u064A۰-۹0-9 ]+$"
test= ("هو الذي يصوركم في الأرحام كيف يشاء لا إله إلا هو العزيز الحكيم \n"
"ربنا إنك جامع الناس ليوم لا ريب فيه إن الله لا يخلف الميعاد \n"
"الصابرين والصادقين والقانتين والمنفقين والمستغفرين بالأسحار \n"
"0123456789 \n")
#Specifying encoding and text behavior
match=re.finditer(reg,test, re.MULTILINE)
print (match)
#looping over lines inside text
for matchNum, match in enumerate(match, start=1):
print("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
并且输出就如预期的一样好:
runfile('C:/Users/Al-Hammad/Documents/PhD/Second Year 2018-2019/Spring Semester 2018-2019/NLP/Assignments/Assignment 1/Arabic NLP.py', wdir='C:/Users/Al-Hammad/Documents/PhD/Second Year 2018-2019/Spring Semester 2018-2019/NLP/Assignments/Assignment 1')
<callable_iterator object at 0x000000000B285BA8>
Match 1 was found at 0-63: هو الذي يصوركم في الأرحام كيف يشاء لا إله إلا هو العزيز الحكيم
Match 2 was found at 64-124: ربنا إنك جامع الناس ليوم لا ريب فيه إن الله لا يخلف الميعاد
Match 3 was found at 125-185: الصابرين والصادقين والقانتين والمنفقين والمستغفرين بالأسحار
Match 4 was found at 186-197: 0123456789
第一个代码的输出必须是在测试文件中找到的匹配项,它可能类似于:
['العربية لغة رسمية في كل دول الوطن العربي إضافة إلى كونها لغة رسمية في تشاد وإريتريا وإسرائيل. وهي إحدى اللغات الرسمية الست في منظمة الأمم المتحدة، ويُحتفل باليوم العالمي للغة العربية في 18 ديسمبر كذكرى اعتماد العربية بين لغات العمل في الأمم المتحدة.[7]']
但是它给了我这个错误,并且没有继续其他段落:
runfile('C:/Users/Al-Hammad/Documents/PhD/Second Year 2018-2019/Spring Semester 2018-2019/NLP/Assignments/Assignment 1/1-4-2019.py', wdir='C:/Users/Al-Hammad/Documents/PhD/Second Year 2018-2019/Spring Semester 2018-2019/NLP/Assignments/Assignment 1')
['اللُّغَة العَرَبِيّة هي أكثر اللغات تحدثاً ونطقاً ضمن مجموعة اللغات السامية، وإحدى أكثر اللغات انتشاراً في العالم، يتحدثها أكثر من 467 مليون نسمة،[4](1) ويتوزع متحدثوها في الوطن العربي، بالإضافة إلى العديد من المناطق الأخرى المجاورة كالأحواز وتركيا وتشاد ومالي والسنغال وإرتيريا وإثيوبيا وجنوب السودان وإيران. اللغة العربية ذات أهمية قصوى لدى المسلمين، فهي عندهم لغة مقدسة إذ أنها لغة القرآن، وهي لغة الصلاة وأساسية في القيام بالعديد من العبادات والشعائر الإسلامية.[5][6]']
Traceback (most recent call last):
File "<ipython-input-41-3249da970121>", line 1, in <module>
runfile('C:/Users/Al-Hammad/Documents/PhD/Second Year 2018-2019/Spring Semester 2018-2019/NLP/Assignments/Assignment 1/1-4-2019.py', wdir='C:/Users/Al-Hammad/Documents/PhD/Second Year 2018-2019/Spring Semester 2018-2019/NLP/Assignments/Assignment 1')
File "C:\Users\Al-Hammad\Anaconda3\envs\py36\lib\site-packages\spyder\utils\site\sitecustomize.py", line 692, in runfile
execfile(filename, namespace)
File "C:\Users\Al-Hammad\Anaconda3\envs\py36\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Al-Hammad/Documents/PhD/Second Year 2018-2019/Spring Semester 2018-2019/NLP/Assignments/Assignment 1/1-4-2019.py", line 21, in <module>
matches=re.finditer(reg,pattern,re.MULTILINE)
File "C:\Users\Al-Hammad\Anaconda3\envs\py36\lib\re.py", line 229, in finditer
return _compile(pattern, flags).finditer(string)
TypeError: expected string or bytes-like object
有人可以告诉我我在做什么错吗?
答案 0 :(得分:0)
您的test.txt
文件可能是utf-8
编码的。如果使用test
编码将问题中的test.txt
字符串复制到utf-8
中并保存,则以下方法将为您提供相同的结果:
import re
reg = r"^[\u0621-\u064A۰-۹0-9 ]+$"
with open('test.txt', encoding='utf-8') as f_input:
test = f_input.read()
# Looping over regex matches inside text
for match_num, match in enumerate(re.finditer(reg, test, re.MULTILINE), start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
即,它给出以下输出:
Match 1 was found at 0-63: هو الذي يصوركم في الأرحام كيف يشاء لا إله إلا هو العزيز الحكيم
Match 2 was found at 64-124: ربنا إنك جامع الناس ليوم لا ريب فيه إن الله لا يخلف الميعاد
Match 3 was found at 125-185: الصابرين والصادقين والقانتين والمنفقين والمستغفرين بالأسحار
Match 4 was found at 186-197: 0123456789
注意:Python f
字符串可用于简化打印语句。