简体   繁体   English

奇怪的“AttributeError:‘NoneType’对象没有属性‘组’”错误

[英]Odd "AttributeError: 'NoneType' object has no attribute 'group'" error

This might be a pretty obvious error since I'm pretty new to coding, but I'm trying to read a file for a certain value which I'll gather by using re.search and splice since I only know the text before and after it.这可能是一个非常明显的错误,因为我对编码还很陌生,但我正在尝试读取某个值的文件,我将使用 re.search 和 splice 收集该值,因为我只知道之前和之后的文本它。

I'm running into a bit of an annoying bug.我遇到了一个烦人的错误。 When I use re.search(r"firstPart(.*?)secondPart", data).group(1) it returns当我使用 re.search(r"firstPart(.*?)secondPart", data).group(1) 它返回

Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
AttributeError: 'NoneType' object has no attribute 'group'

Which is a problem with this line:这条线有什么问题:

englishWord = re.search(r"<i>(.*?)</i>", str(englishWord)).group(1)

If you read the code, you can see that I've made some unnecessary lines where instead of writing the entire string in the re.search function I'll use only a little bit then add or remove text in another function.如果您阅读代码,您会发现我做了一些不必要的行,而不是在 re.search 函数中写入整个字符串,我将只使用一点,然后在另一个函数中添加或删除文本。 This is because if I do it all in the re.search function normally it doesn't work.这是因为如果我在 re.search 函数中完成所有操作,通常它不起作用。

Possibly the most annoying part and confusing about this is that if I run everything before "englishWord = re.search(r" (.*?) ", str(englishWord)).group(1)" then I run it, it works, but if I run all of the code at once I get that error.可能最烦人和最令人困惑的部分是,如果我在“englishWord = re.search(r” (.*?) “, str(englishWord)).group(1)” 之前运行所有内容,然后我运行它,它会起作用,但是如果我一次运行所有代码,我就会得到那个错误。 Any idea why?知道为什么吗? How can I fix this?我怎样才能解决这个问题? Thanks!谢谢! (I am using python 3.6) (我正在使用 python 3.6)

My Code vvv我的代码 vvv

#!/Library/Frameworks/Python.framework/Versions/3.6/bin/python3

import re
import itertools

with open('Desktop/data.txt', 'r') as myfile:
    data=myfile.read().replace('\n', '')

num = 0

for x in itertools.repeat(None, 8):
    num = int(num) + 1
    if int(num) < 10:
        num = '0' + str(num)
    firstString = re.search(r"id=\"question_" + num + "_whole_question\" data-sidebar-reference=\"\">    (.*?) <input", data).group(1)
    secondString = re.search(r"id=\"question_" + num + "_wol_1\"(.*?)  </div>", data).group(1)
    secondString = secondString.replace(" name=\"question_" + num + "_wol_1\" onchange=\"has_unsaved_work();\" size=\"10\" type=\"text\" />", "")
    finalString = firstString + " _" + secondString
    englishWord = re.search(r"(<i><span lang=\"en-US\">(.*?)</span></i>)", finalString)
    englishWord = re.search(r"<i>(.*?)</i>", str(englishWord)).group(1)
    englishWord = "<i>" + englishWord + "</i>"
    finalString = finalString.replace(englishWord, "")
    finalString = finalString.replace("()", "")
    print (finalString)

call group only if there is a match.仅当存在匹配时才呼叫组。

res = re.search(r"<i>(.*?)</i>", str(englishWord))
# if there is a match
if res:
   englishWord = res.group(1)

As pointed out in the comments, re.search returns None when no match is found.正如评论中指出的那样,当找不到匹配项时, re.search返回 None。 Link : https://docs.python.org/3/library/re.html#re.search链接: https : //docs.python.org/3/library/re.html#re.search

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:&#39;NoneType&#39;对象没有属性&#39;group&#39;错误 - AttributeError: 'NoneType' object has no attribute 'group' error Python GTTS 错误:AttributeError:&#39;NoneType&#39; 对象没有属性 &#39;group&#39; - Python GTTS Error : AttributeError: 'NoneType' object has no attribute 'group' AttributeError:“ NoneType”对象没有属性“ group” {for instagram} - AttributeError: 'NoneType' object has no attribute ‘group’ {for instagram} AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;group&#39; , - AttributeError: 'NoneType' object has no attribute 'group' , AttributeError:&#39;NoneType&#39;对象没有属性&#39;group&#39; - AttributeError: 'NoneType' object has no attribute 'group' AttributeError: 'NoneType' object 没有属性 'group - AttributeError: 'NoneType' object has no attribute 'group AttributeError: 'NoneType' object 在 googletrans 中没有属性 'group' - AttributeError: 'NoneType' object has no attribute 'group' in googletrans AttributeError: 'NoneType' object 没有属性 'group' - AttributeError: 'NoneType' object has no attribute 'group' AttributeError:&#39;NoneType&#39;对象没有属性&#39;group&#39; - AttributeError: 'NoneType' object has no attribute 'group' AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;group&#39; 问题 - AttributeError: 'NoneType' object has no attribute 'group' problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM