查找,拆分和连接

时间:2014-10-02 07:50:12

标签: python split find concatenation

我必须在第二行(17.位置)找到某个供应商编号

例如,我必须找到,拆分和连接这种类型的文本 - (查找,拆分和连接的说明符是第二行 - NUMBER,其中包含 从6个数字,所以我必须找到这个数字,并根据这个数字连接)

我必须使用某种正则表达式吗?或者只找到,拆分和连接? (根据数字 - 45107,57107)

输出也在这里:

Add and Remove Platforms.
             47107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             47107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             57107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             47107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             57107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.

输出(查找,拆分和连接后):

Add and Remove Platforms.
             47107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             47107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             47107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             57107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             57107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.

1 个答案:

答案 0 :(得分:0)

您似乎希望输出按数字排序,以便您可以使用re.split并使用lambda排序,如果所有输出都已过帐:

s = """
Add and Remove Platforms.
             47107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             47107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             57107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             47107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             57107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
"""
import re

srt = sorted(re.split("(?<=which CPython is used.)\n",s),key=lambda x: re.findall("\d{5}",x))

for line in srt:
   print line

Add and Remove Platforms.
             47107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             47107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             47107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             57107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.
Add and Remove Platforms.
             57107 Specify which Python runtime, CPython or Jython, to use as a
Choose which CPython is used.