Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python和库中的不平衡括号错误_Python_Href_Re - Fatal编程技术网

Python和库中的不平衡括号错误

Python和库中的不平衡括号错误,python,href,re,Python,Href,Re,我想将我的hrefs删除到我的数据集中,但我得到了以下错误:“不平衡括号”! 要删除“href”,我使用以下python代码: data=data.apply(lambda x:re.sub(re.findall(r'\',x)[0],'',x)if(len(re.findall(r'\',x))>0)和('href'在re.findall(r'\',x)[0])else x) 在这个应用程序之后,我得到以下错误: /usr/local/lib/python3.6/dist-packages/

我想将我的
href
s删除到我的数据集中,但我得到了以下错误:“不平衡括号”! 要删除“href”,我使用以下python代码:

data=data.apply(lambda x:re.sub(re.findall(r'\',x)[0],'',x)if(len(re.findall(r'\',x))>0)和('href'在re.findall(r'\',x)[0])else x)
在这个应用程序之后,我得到以下错误:

/usr/local/lib/python3.6/dist-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   4211             else:
   4212                 values = self.astype(object)._values
-> 4213                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   4214 
   4215         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-25-55819437c264> in <lambda>(x)
----> 1 data = data.apply(lambda x: re.sub(re.findall(r'\<a(.*?)\>', x)[0], '', x) if (len(re.findall(r'\<a (.*?)\>', x))>0) and ('href' in re.findall(r'\<a (.*?)\>', x)[0]) else x)
      2 if verbose: print('#'*10 ,'Step - Remove hrefs:'); check_vocab(data, local_vocab)

/usr/lib/python3.6/re.py in sub(pattern, repl, string, count, flags)
    189     a callable, it's passed the match object and must return
    190     a replacement string to be used."""
--> 191     return _compile(pattern, flags).sub(repl, string, count)
    192 
    193 def subn(pattern, repl, string, count=0, flags=0):

/usr/lib/python3.6/re.py in _compile(pattern, flags)
    299     if not sre_compile.isstring(pattern):
    300         raise TypeError("first argument must be string or compiled pattern")
--> 301     p = sre_compile.compile(pattern, flags)
    302     if not (flags & DEBUG):
    303         if len(_cache) >= _MAXCACHE:

/usr/lib/python3.6/sre_compile.py in compile(p, flags)
    560     if isstring(p):
    561         pattern = p
--> 562         p = sre_parse.parse(p, flags)
    563     else:
    564         pattern = None

/usr/lib/python3.6/sre_parse.py in parse(str, flags, pattern)
    867     if source.next is not None:
    868         assert source.next == ")"
--> 869         raise source.error("unbalanced parenthesis")
    870 
    871     if flags & SRE_FLAG_DEBUG:

error: unbalanced parenthesis at position 36
/usr/local/lib/python3.6/dist-packages/pandas/core/series.py在应用中(self、func、convert\u dtype、args、**kwds)
4211其他:
4212值=self.astype(对象)。\u值
->4213 mapped=lib.map\u推断(值,f,convert=convert\u数据类型)
4214
4215如果len(映射)和isinstance(映射[0],系列):
pandas/_libs/lib.pyx在pandas中。_libs.lib.map_infere()
in(x)
---->1 data=data.apply(lambda x:re.sub(re.findall(r'\',x)[0],'',x)if(len(re.findall(r'\',x))>0)和('href'在re.findall(r'\',x)[0])else x)
2如果详细:打印(“#”*10,“步骤-删除HREF:”);检查语音(数据、本地语音)
/sub中的usr/lib/python3.6/re.py(模式、repl、字符串、计数、标志)
189一个可调用函数,它传递了match对象并且必须返回
190要使用的替换字符串。”“”
-->191返回编译(模式、标志).sub(repl、字符串、计数)
192
193 def子网(模式、应答、字符串、计数=0、标志=0):
/编译中的usr/lib/python3.6/re.py(模式、标志)
299如果不是sre_compile.isstring(模式):
300 raise TypeError(“第一个参数必须是字符串或编译模式”)
-->301 p=sre_compile.compile(模式、标志)
302如果不是(标志和调试):
303如果len(\u cache)>=\u MAXCACHE:
/编译中的usr/lib/python3.6/sre_compile.py(p,标志)
560如果是字符串(p):
561模式=p
-->562 p=sre_parse.parse(p,标志)
563其他:
564模式=无
/解析中的usr/lib/python3.6/sre_parse.py(str、标志、模式)
867如果source.next不是None:
868断言源代码。下一步==“””
-->869上升源错误(“不平衡括号”)
870
871如果标志和SRE_标志调试:
错误:位置36处括号不平衡
经过几个小时的练习,我对解决这个问题有了任何想法。

您的代码包含

re.sub(re.findall(...))
re.findall
自本文档起执行以下操作:

返回字符串中所有非重叠匹配项的列表


但是
re.sub
需要一个模式作为它的第一个参数,但是却得到了一个包含html的字符串。这就是它抛出正则表达式编译错误的原因。
re.sub()
的第一个参数是正则表达式。
re.findall()返回的字符串
不是正则表达式,它们是在
x
中找到的字符串。如果这恰好是一个有效的regexp,并且它也与您想要的匹配,那将是非常巧合的

如果要替换所有的
,只需将其用作
re.sub()
中的regexp参数。那么无需使用条件检查表达式是否匹配;如果不匹配,
re.sub()
将只返回未更改的字符串


您还应该检查
之后是否有独立于正则表达式的空格,尝试使用
.apply()
模式,这对于lambda来说太多了。将逻辑提取到适当的函数中,以便我们可以看到它。仅供参考,您不需要在regexp中转义
。他们使用的是
[0]
获取列表中的第一个元素。@Barmar是的,你是对的。谢谢。但它仍然是一个包含
html
的字符串。因此也不起作用。不过更改了我的答案。你好,skyrocketer。你是在追求名誉吗?不是每个人都在以某种方式追求名誉吗?:D这有问题吗?不。人们不是在追求名誉吗为名誉(大多数人)而生气。请参阅
data = data.apply(lambda x: re.sub(r'<a\s.*?>', '', x, flags=re.IGNORECASE))
data = data.str.replace(r'<a\s.*?>', '', flags=re.IGNORECASE)