with open('file100', 'r') as f:
for line in f:
print(line.rsplit(None, 1)[1])
输出:
^{pr2}$
rsplit(None,1)只意味着我们从空白字符串的末尾分离一次,然后再提取第二个元素:
In [1]: s = "Hi this doller 100.00"
In [2]: s.rsplit(None,1)
Out[2]: ['Hi this doller', '100.00']
In [3]: s.rsplit(None,1)[1]
Out[3]: '100.00'
In [4]: s.rsplit(None,1)[0]
Out[4]: 'Hi this doller'
如果您确实需要regex使用search:
import re
with open('file100', 'r') as f:
for line in f:
m = re.search(r"\b\d+\.\d{2}\b",line)
if m:
print(m.group())
import re
file = open('file.txt', 'r')
for digit in file.readlines():
myre = re.search(r'\s\b(\d*\.\d{2})\b', digit)
if myre:
print myre.group(1)
结果
^{pr2}$
来自文档:
Scan through string looking for the first location where the regular
expression pattern produces a match
如果您决定使用一个组,还需要括号:
(...) Matches whatever regular expression is inside the parentheses,
and indicates the start and end of a group; the contents of a group
can be retrieved after a match has been performed, and can be matched
later in the string with the \number special sequence, described
below. To match the literals '(' or ')', use ( or ), or enclose them
inside a character class: [(] [)].
String literals may optionally be prefixed with a letter 'r' or 'R';
such strings are called raw strings and use different rules for
interpreting backslash escape sequences.
。。。在
Unless an 'r' or 'R' prefix is present, escape sequences in strings
are interpreted according to rules similar to those used by Standard C
如果它们总是在行尾,只需
rsplit
一次,然后拉出最后一个元素:输出:
^{pr2}$rsplit(None,1)
只意味着我们从空白字符串的末尾分离一次,然后再提取第二个元素:如果您确实需要regex使用
search
:请改用re.search:
结果
^{pr2}$来自文档:
如果您决定使用一个组,还需要括号:
re.match仅在以下情况下有效:
r
将regex
括为raw strings:。。。在
您的主要问题是使用},后者允许从字符串中的任何一点开始匹配。不过,我会把我的建议分解如下:
re.match
,它需要从字符串开头开始的匹配,而不是{不需要在每个循环上重新编译(Python实际上会为您缓存一些regex,但是为了安全起见,在引用中保留一个regex)。我使用
^{pr2}$VERBOSE
标志来为您分解regex。在字符串前面使用r
,这样反斜杠就不会在Python读取字符串时转义它们前面的字符:使用上下文管理器,用通用换行符打开文件,
'rU'
模式,这样无论文件是在哪个平台上创建的,都可以逐行读取。在不要使用
readlines
,它会一次将整个文件加载到内存中。相反,请将file对象用作迭代器:我的代码打印:
相关问题 更多 >
编程相关推荐