我正试图找到文字和打印使用下面的代码。一切都很完美,但唯一的问题是我无法打印最后一个字(即数字)
words = ['Town of','Block No.','Lot No.','Premium (if any) Paid ']
import re
for i in words:
y = re.findall('{} ([^ ]*)'.format(i), textfile)
print(y)
我正在使用的文本文件:
textfile = """1, REBECCA M. ROTH , COLLECTOR OF TAXES of the taxing district of the
township of MORRIS for Six Hundred Sixty Seven dollars andFifty Two cents, the land
in said taxing district described as Block No. 10303 Lot No. 10 :
and known as 239 E HANOVER AVE , on the tax Taxes For: 2012
Sewer
Assessments For Improvements
Total Cost of Sale 35.00
Total
Premium (if any) Paid 1,400.00 """
我想知道我在哪里犯了错误。 如有任何建议,我们将不胜感激
两个问题:
'Premium (if any) Paid '
字符串以空格结尾,并且'{} ([^ ]*)'
在{}
之后还有一个空格,这将它们相加。删除'Premium (if any) Paid '
中的尾随空格李>['Premium \(if any\) Paid']
。您也可以改用re.escape
李>对于您的特殊情况,这似乎是一个最佳解决方案:
有几个问题:
(
{.
。非常简单,您可以使用^{Premium \(if any\) Paid
中的尾随空格(它试图匹配两个空格,而不是一个,因为您也在检查正则表达式{} ([^ ]*)
中的空格)您应该改为将代码更改为以下内容:
See working code here
相关问题 更多 >
编程相关推荐