为什么're.finditer'返回错误的字符索引



我有这段文字。我想找到每次发生"PO 00000"时的字符串索引。

我正在使用findPageNum = [m.start() for m in re.finditer(pageNum, noNewLine)]

但它给了我一个值,即 "PO 00000" 开始后 20 个字符。

这是我的代码。

# -*- coding: utf-8 -*-
import re
noNewLine = ("UR  IB U  U  E PL  NU  Congressional Record  M  S  United States of America  PROCEEDINGS AND DEBATES OF THE  Vol. 163  115 th CONGRESS, FIRST SESSION  WASHINGTON, WEDNESDAY, JUNE 7, 2017  No. 97  Senate The Senate met at 9:30 a.m. and was called to order by the Honorable RAND PAUL, a Senator from the Commonwealth of Kentucky. f  PRAYER The Chaplain, Dr. Barry C. Black, offered the following prayer: Let us pray. Father of love, whose presence and power is revealed to the hearts that long for Your guidance, we thank You for the gift of this day. May we use this borrowed time for Your glory. As our lawmakers strive to honor You, may they work with commendable zeal, knowing that life’s evening is coming when their labor will be done. Lord, give them the wisdom to keep Your words in their hearts, providing them with a lamp for their feet and a light for their paths. Continue to be our strength and shield. May we think of You consistently and trust You constantly. We pray in "
"Your loving Name. Amen. f  PLEDGE OF ALLEGIANCE The Presiding Officer led the Pledge of Allegiance, as follows: I pledge allegiance to the Flag of the United States of America, and to the Republic for which it stands, one nation under God, indivisible, with liberty and justice for all. f  Mr. PAUL thereupon assumed the Chair as Acting President pro tempore. f  RECOGNITION OF THE MAJORITY LEADER The ACTING PRESIDENT pro tempore. The majority leader is recognized. f  MEASURE READ THE FIRST TIME—H.R. 1628 Mr. MCCONNELL. Mr. President, I understand there is a bill at the desk, and I ask for its first reading. The ACTING PRESIDENT pro tempore. The clerk will read the bill by title for the first time. The legislative clerk read as follows: A bill (H.R. 1628) to provide for reconciliation pursuant to title II of the concurrent resolution on the budget for fiscal year 2017.  Mr. MCCONNELL. Mr. President, I now ask for a second reading and, in order to place the bill on the calendar under "
"the provisions of rule XIV, I object to my own request. The ACTING PRESIDENT pro tempore. Objection is heard. The bill will be read for the second time on the next legislative day. f  APPOINTMENT OF ACTING PRESIDENT PRO TEMPORE The PRESIDING OFFICER. The clerk will please read a communication to the Senate from the President pro tempore (Mr. HATCH). The legislative clerk read the following letter: U.S. SENATE, PRESIDENT PRO TEMPORE, Washington, DC, June 7, 2017. To the Senate: Under the provisions of rule I, paragraph 3, of the Standing Rules of the Senate, I hereby lotter on DSKBCFDHB2PROD with SENATE  appoint the Honorable RAND PAUL, a Senator from the Commonwealth of Kentucky, to perform the duties of the Chair. ORRIN G. HATCH, President pro tempore.  SANCTIONS LEGISLATION Mr. MCCONNELL. Mr. President, Senators today will have an opportunity to advance important bipartisan Iran sanctions legislation. As we consider this bill, we anticipate that amendments addressing Russia "
"sanctions are likely to be offered. I am encouraged that the chairmen of the Foreign Relations and Banking Committees, Senator CORKER and Senator CRAPO, have already been in discussions with their respective ranking  members to work toward a bipartisan agreement. I support that effort, and I will have more to say about the underlying legislation tomorrow. f  INFRASTRUCTURE Mr. MCCONNELL. Now, Mr. President, on one other matter, later today President Trump will visit Cincinnati to discuss the importance of our Nation’s inland waterways. Kentucky is home to over 1,900 miles of navigable inland waterways, which, in addition to adding majestic beauty to my State, are also vital to thousands of jobs in the Commonwealth. In recent years, over 95 million tons of cargo and agricultural products have been transported across these water trade routes. Our many levees, docks, and dams represent crucial infrastructure that play an important role in our regional and national economy. I am proud "
"of the work Congress has done in the past to protect our Nation’s waterways, like passing the Water Resources Development Act by a bipartisan majority last year in order to support infrastructure, enhance commerce, and maintain American ecosystems. As President Trump continues to release his plans for our Nation’s infrastructure, I look forward to working with the administration and colleagues in the Senate to protect and improve the many roads, bridges, airports, and waterways that serve people and jobs all across our country. f  ORDER OF PROCEDURE Mr. MCCONNELL. Mr. President, I ask unanimous consent that notwithstanding rule XXII, the cloture vote on the motion to proceed to S. 722 occur at 1:45 p.m. today, and if cloture is invoked, time postcloture count as if invoked at 10:30 a.m. The ACTING PRESIDENT pro tempore. Without objection, it is so ordered.  ∑ This ‘‘bullet’’ symbol identifies statements or insertions which are not spoken by a Member of the Senate on the floor. "
"S3301  . VerDate Sep 11 2014  23:29 Jun 07, 2017  Jkt 069060  PO 00000  Frm 00001  Fmt 0624  Sfmt 0634  E:CRFMA07JN6.000  S07JNPT1 PO 00000  Frm 00001  Fmt 0637  Sfmt 0634  E:CRFMA05JN6.000  S05JNPT1")
pageNum = "PO 00000"
findPageNum = [m.start() for m in re.finditer(pageNum, noNewLine)]
print(findPageNum)

输出为 [5040, 5112] 。应该是[5020, 5092].

你似乎认为你在 Python 3 上,但你在 Python 2 上。(无论如何,你正在编写print调用,就像你认为你在 Python 3 上一样。

在 Python 2 上,你的字符串是一个字节串,你使用的是字节串正则表达式。所有索引都是按字节,而不是按字符。字节串包含 ASCII 范围之外的 Unicode 字符的多字节 UTF-8 编码,导致索引与预期不同。

获取 Python 3。

相关内容

  • 没有找到相关文章

最新更新