python中的grepl函数
我想重新创建一个从R到Python的代码片段。我有一个来自R(Python中的数据帧)的TIBLE,它看起来像这样:python中的grepl函数,python,r,grepl,Python,R,Grepl,我想重新创建一个从R到Python的代码片段。我有一个来自R(Python中的数据帧)的TIBLE,它看起来像这样: column1 column2 column3 amsterdam het dag calamiteit bij doen gratis dag 2013 rotterdam blijdorp groet gratis burp het ik ben 2015 root wit g
column1 column2 column3
amsterdam het dag calamiteit bij doen gratis dag 2013
rotterdam blijdorp groet gratis burp het ik ben 2015
root
wit
geel
with
asd
goed
black
red
[None,
None,
None,
None,
None,
None,
<re.Match object; span=(0, 1), match=' '>,
None,
<re.Match object; span=(0, 0), match=''>,
<re.Match object; span=(0, 1), match=' '>]
使用下面的代码,我试图将描述提取为单个字符串。
代码如下:
#R code
for (i in 1:nrow(tibble)) {
des <- pull(tibble[i,2])
}
#Python code
for i in df:
des = df['column2'].str.split(expand=True).stack()
但是,然后我想将这段代码从R重新创建到Python,我不知道如何:
if (grepl("^\\s*$", des) == TRUE) { # if description is only whitespace then skip
trns <- tibble(translatedText = "", detectedSourceLanguage = "", text = "")
if(grepl(“^\\s*$”,des)=TRUE){如果描述仅为空白,则跳过
trnsgrepl的近似等价物是re.match。
看看这个小例子:
import re
data = ["00het", "dags"]
matches = [re.match(r"\d{2}", str_) for str_ in data]
虽然第一个字符串有匹配项,但另一个字符串没有,因为其中没有两个数字。
我希望这可能是您将表达式从R转换为python的一个很好的起点我得到了一个从上面重新创建R脚本的完美方法。这是python代码:
if [re.match(r'^\s*$', i) for i in des]:
trns = i
如果我有一系列的字符串,像这样:
column1 column2 column3
amsterdam het dag calamiteit bij doen gratis dag 2013
rotterdam blijdorp groet gratis burp het ik ben 2015
root
wit
geel
with
asd
goed
black
red
[None,
None,
None,
None,
None,
None,
<re.Match object; span=(0, 1), match=' '>,
None,
<re.Match object; span=(0, 0), match=''>,
<re.Match object; span=(0, 1), match=' '>]
然后在我用if语句运行它之后,我会得到如下结果:
column1 column2 column3
amsterdam het dag calamiteit bij doen gratis dag 2013
rotterdam blijdorp groet gratis burp het ik ben 2015
root
wit
geel
with
asd
goed
black
red
[None,
None,
None,
None,
None,
None,
<re.Match object; span=(0, 1), match=' '>,
None,
<re.Match object; span=(0, 0), match=''>,
<re.Match object; span=(0, 1), match=' '>]
[无,
没有一个
没有一个
没有一个
没有一个
没有一个
,
没有一个
,
]
这是否回答了您的问题?R中的(“^\\s*$”)在Python中等于什么?@Felixregex是独立于语言的。Python特有的是,您有一个“R”前缀来标记正则表达式字符串。