【问题标题】:Python extracting specific substringPython提取特定子字符串
【发布时间】:2021-11-08 04:10:57
【问题描述】:

所以我在 Python 中有以下字符串:

teststr = "First Line.............................234" \
          "1.1.0 (L1) TestLine.........................567" \
          "1.1.1 (L1) Second Line.............................587"\
          "Third Line.............................856" \
          "1.1.2 (L2) Fourth Line.............................775"\
          "1.2.7 (L1) Fifth Line.............................262" \
          "1.5.3 (L1) Sixth Line .............................346"\
          "Seventh Line..............................234"

我只需要将 (L1) 中的信息保存在列表中。

我无法遍历行(如果行包含 L1 或其他内容),因为有时 (L1) 信息需要的信息超过行(例如第二行第三行)。

我尝试了很多尝试再次拆分和加入字符串,但对我没有任何帮助。

有人知道我该怎么做吗?

【问题讨论】:

  • 行与行之间是否有换行符?预期的输出是什么?
  • 预期输出为:[Testline, Second LineThird Line, Fifth Line, Sixth LineSeventh Line] 之间没有换行符。
  • Edit 发布具有预期输出且不难阅读的 cmets。

标签: python string list split


【解决方案1】:

您可以在正则表达式上拆分字符串,然后循环遍历数据:

teststr = "First Line.............................234" \
          "1.1.0 (L1) TestLine.........................567" \
          "1.1.1 (L1) Second Line.............................587"\
          "Third Line.............................856" \
          "1.1.2 (L2) Fourth Line.............................775"\
          "1.2.7 (L1) Fifth Line.............................262" \
          "1.5.3 (L1) Sixth Line .............................346"\
          "Seventh Line..............................234"
import re
results = re.split(r'(\(L\d+\))',teststr)

这会将输入拆分为类似于(Ln) 的任何值,其中n 可以是任何数字。

它给出了一个包含以下值的列表:

['First Line.............................2341.1.0 ',
 '(L1)',
 ' TestLine.........................5671.1.1 ',
 '(L1)',
 ' Second Line.............................587Third Line.............................8561.1.2 ',
 '(L2)',
 ' Fourth Line.............................7751.2.7 ',
 '(L1)',
 ' Fifth Line.............................2621.5.3 ',
 '(L1)',
 ' Sixth Line .............................346Seventh Line..............................234']

在这种情况下,我们只想选择(L1) 之后的值,因此我们在列表上循环(滑动)并仅在它出现在(L1) 之后才打印该值。

for x, y in zip(results, results[1:]):
  if x == '(L1)':
    print(y)

完整的代码变成:

teststr = "First Line.............................234" \
          "1.1.0 (L1) TestLine.........................567" \
          "1.1.1 (L1) Second Line.............................587"\
          "Third Line.............................856" \
          "1.1.2 (L2) Fourth Line.............................775"\
          "1.2.7 (L1) Fifth Line.............................262" \
          "1.5.3 (L1) Sixth Line .............................346"\
          "Seventh Line..............................234"
import re
results = re.split(r'(\(L\d+\))',teststr)

for x, y in zip(results, results[1:]):
  if x == '(L1)':
    print(y)

这给出了:

 TestLine.........................5671.1.1 
 Second Line.............................587Third Line.............................8561.1.2 
 Fifth Line.............................2621.5.3 
 Sixth Line .............................346Seventh Line..............................234

【讨论】:

  • 这很有帮助,谢谢!你能(如果你愿意的话)解释一下这是如何工作的吗?
  • @IRezzet 添加了一些解释,如果还有什么不清楚的地方请告诉我。
  • 谢谢您,这非常有帮助且易于理解。也感谢您的快速帮助!
【解决方案2】:
s = '(L1)'
for i in teststr.split('1.'):
    n = i.find(s)
    if n != -1:
        print(i[n+len(s):])

输出:

 TestLine.........................567
 Second Line.............................587Third Line.............................856
 Fifth Line.............................262
 Sixth Line .............................346Seventh Line..............................234

正如您在问题下的 cmets 中所示,如果您想要一个列表:

s = '(L1)'
lines = [i[n+len(s):]for i in teststr.split('1.') if (n := i.find(s)) != -1]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多