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

2024-07-07 07:04:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这段文字。我想找到每次"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:\CR\FM\A07JN6.000  S07JNPT1 PO 00000  Frm 00001  Fmt 0637  Sfmt 0634  E:\CR\FM\A05JN6.000  S05JNPT1")

pageNum = "PO 00000"
findPageNum = [m.start() for m in re.finditer(pageNum, noNewLine)]
print(findPageNum)

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


Tags: andofthetoinforthatis
1条回答
网友
1楼 · 发布于 2024-07-07 07:04:57

您似乎认为自己使用的是python3,但是您使用的是python2(无论如何,您编写的print调用就像您使用的是python3一样)

在python2上,您的字符串是bytestring,您使用的是bytestring regex。所有索引都是按字节,而不是按字符。bytestring包含位于ASCII范围之外的Unicode字符的多字节UTF-8编码,导致索引与预期的不同

获取Python 3

相关问题 更多 >