我对python很陌生,在运行python的raspberry pi上捕捉多行文本块时遇到了问题。
我正在尝试捕捉HELLO和WORLD之间的多行文本。
此示例引发错误AttributeError:'NoneType‘对象没有属性'group’
linestring = """
TEST TEST HELLO
TEST TEST PIZZA
TEST TEST WORLD
TEST TEST
"""
print(linestring)
m = re.search('HELLO(.*)WORLD', lines
有没有办法获得哪个组与正则表达式匹配?
例如(使用js,但可以用Python等重新表示):
`
myRegex = /(this)|(that)|(other)/ig //describes 3 group captures
// The groups number (this==0, that==1, other==2) --> I'd like to get the capturing group number returned in the matches.
myString = "this is awesome cause that is the oth
我正在使用Python来(1)访问一个xml文件,(2)搜索包含regex1的节点,(3)搜索为regex2找到的节点(它有一对捕获组),然后(4)与组一起执行操作。
我有第一步和第二步的工作。但我被困在了3和4上。下面是我的代码示例:
from bs4 import BeautifulSoup
from urllib import urlopen
import re
from lxml import etree
url='https://www.gpo.gov/fdsys/bulkdata/BILLS/113/1/hr/BILLS-113hr2146ih.xml'
soup
我想在Python中提取组名和与之相关的数字。
示例输入:
34 patients have admitted in hospital and distributed in Pune group with 20 patients, Mumbai group with 10 patients and Nagpur group with 4 patients.
示例输出:
'Pune group, 20'
'Mumbai group, 10'
'Nagpur group, 4'
在使用list和regex查询mongodb模型(Python/MongoEngine)时,我得到了异常。代码是
from mongoengine import *
import re
db = connect('testdb')
class Team(Document):
name = StringField()
groups = ListField(ReferenceField('Group'))
class Group(Document):
name = StringField()
Team.drop_collection
我必须匹配python.中的多行
group one start
line 1 data
group end
group two start
group two data
group end
在上面的字符串中如何获得输出下面的
[group one start \n line 1 data \n group end, group two start \n group two data \n group end]
--我在下面的代码中尝试过,但没有使用
import re
re.findall(r'group.*start.*group end',re.MULTILINE |
我做了一个编码来打印ext (“和内部"),但是我得到了错误。
我的意思是,如果我有文本:hello everyone("Can you see"),那么它必须打印为Can you see
我的编码:
>>> import re
>>> s = "<_io.TextIOWrapper ('C:/Python34/abcd.txt') mode='w' encoding='cp1252'>"
>>> m = re.search(r"(?
我用Python编写了一个正则表达式来获取字符串中的数字。但是,当我运行match.group()时,它说对象list没有属性group。我做错了什么?我输入的代码粘贴到终端,以及终端的响应。谢谢。
>>> #import regex library
... import re
>>>
>>> #use a regex to just get the numbers -- not the rest of the string
... matcht = re.findall(r'\d', dwtunl)
>>&g
我有一个文本块如下:
group 1
name A
name B
name C
group 2
name X
name Y
name Z
group 3
name I
name II
name III
它是一个嵌套组(第1组,第2组,第3组,.),每个组都包含子组(name x,name y,name z,.)。如何使用regex将这个嵌套组和子组与python匹配?
下面我尝试查找所有模式来匹配外部组,但是如何添加更多的代码来匹配子组呢?
import re
content = """
group 1
name A
name B
name C
group 2
nam
我正在尝试打印我的正则表达式匹配中的组信息。我的脚本匹配我的文件中的regex‘s行,所以这是有效的。
我基于python regex教程btw…我是python新手(有一些perl经验) :)
import re
file = open('read.txt', 'r')
p = re.compile("""
.*,\\\"
(.*) # use grouping here with brackets so we can fetch value with group later on
\\\"
我有两个字符串:
a = 'B E R L I N IS A CITY'
b = 'PARIS IS A CITY, TOO'
我希望匹配第一个单词,以防它只有一个空格,或者是一个预定义的单词。
我想出的正则表达式(Python)是
regex = re.compile('^(?P<city>([a-z] )*|(paris )).*$', re.IGNORECASE)
print regex.match(a).group('city'), regex.match(b).group('city')
&g
我不得不用不动点替换所有的科学符号实例,并编写了一个Python脚本来帮助解决问题。下面是:
#!/usr/bin/python2
# Originally written by Anton Golov on 29 Jan 2012.
# Feedback can be sent to <email> .
"""Provide functions for converting scientific notation to fixed point.
When imported, reads lines from stdin and prints them
我对Python很陌生,我想用python运行一段代码。
我编写的代码是
#!/usr/bin/python
import sys
import re
for line in sys.stdin:
results = re.search('ipAddress=([^&]*])', line)
if len(results.group(1)) != 0:
print results.group(1)
需求是在string.String中查找IP地址如下所示
blah....ipAddress=173.110.208.118&d
我有一个很大的字符串,它实际上包含了很多其他的子字符串。当我运行我的代码时,它不能正确地扫描字符串并返回false值。
目标:我正在尝试搜索字符串中的特定值,并希望将其存储到变量中。
Code`import re
mystring = "This 'is' the 'test' I am doing"
searchObj = re.search( r'(.*) test (.*?) .*', mystring, re.M|re.I)
if searchObj:
print "searchObj.group() :
我对Regex非常陌生,我在javascript中搜索了很长一段时间的等价物,我希望有人能对从python转换而来的javascript中的regex做出详细的解释。
import re
regex = r"""
^(
(?P<ShowNameA>.*[^ (_.]) # Show name
[ (_.]+
( # Year with possible Season and Episode
(?P<ShowYearA>\d{4})
([ (_.]+S(?
我试图将我的python代码(Binance Bot)上传到Heroku上,但是出现了一个сс编辑错误。有人能帮帮我吗?
from binance.client import Client
from datetime import datetime
client = Client(api,key)
symbol = 'IOSTUSDT'
for i in client.futures_historical_klines(symbol, Client.KLINE_INTERVAL_1MINUTE, '2022-03-16'):
print(i)
错误是
请容忍我这一点,因为我可能解释得不太好。
我有一个简单的正则表达式:
^(The\s)?(cat\s)?(sat\s)?(on\s)?(the\s)?(mat\.)?
将文本转换为
The cat sat on the mat.
成功通过。太棒了!
然而,我所追求的是一种找出regex在哪些组上失败的方法。例如:
The cat sat on the mat # fails on group 6 (no period)
The cat sat on teh mat. # fails on group 5 (teh instead of the)
The kat sat on the mat.
我是python扩展Regex的新手。我试图做一个迷你python项目,我遇到了一个问题。我在试着做电话号码检查。如果你输入一个适当的号码,你会收到一个“谢谢”,但如果你输入随机号码(不是电话号码)或随机字母,那么你应该收到一个‘再试’。
问题是,如果我输入一个字母,那么我会收到一个AttributeError:'NoneType‘对象没有属性’‘。
我怎样才能解决这个问题?任何帮助都会很好!谢谢你花时间看这个。
巴斯丁:
代码:
import re
import time
print('Welcome, you have won 1 million dollars! Pl
我正在尝试使用regexp从所选字符串搜索中删除所有空格。代码可以工作,但它继续返回一个我不确定如何解决的错误...?
elif searchType =='2':
print " Directory to be searched: c:\Python27 "
directory = os.path.join("c:\\","SQA_log")
userstring = raw_input("Enter a string name to search: ")
use