在Python中分隔字符串和整数

时间:2017-04-10 12:38:36

标签: python

我想分割字符串和数字。因此,如果连接的字符串是:

Hans went to house number 10 92384 29349

它应该将文本分成:

Hans went to house number | 10 | 92384 | 29349

我对如何解决这个问题感到困惑,因为它会分裂Hans |去了到|房子|数..

6 个答案:

答案 0 :(得分:6)

使用正则表达式非常简单:

>>> import re
>>> s = "Hans went to house number 10 92384 29349"
>>> re.split(r'\s+(?=\d+\b)', s)
['Hans went to house number', '10', '92384', '29349']

那说你的问题很混乱,如果你想将|字符添加到输出中,只需再次加入输出:

>>> ' | '.join(_)
'Hans went to house number | 10 | 92384 | 29349'

如果你的目标是实现一个能够解决问题的功能,你可以这样写:

def split_numbers(string, join=None):
   from re import split
   split = re.split(r'\s+(?=\d+\b)', string)
   return join.join(split) if join else split

请注意,我在正则表达式中添加了边界\b,以避免匹配句子2cups中的Hans went to house number 10 92384 29349 and drank 2cups of coffee之类的数字{/ 1}}

答案 1 :(得分:3)

如果您只想将|添加到字符串中,可以尝试:

a="Hans went to house number 10 92384 29349"

print(" ".join("| "+i if i.isdigit() else i for i in a.split()))

输出:

Hans went to house number | 10 | 92384 | 29349

答案 2 :(得分:2)

您可以将句子拆分为单词,然后尝试将单词转换为整数。如果演员表失败,那么只需连接

a = "Hans went to house number 10 92384 29349"
res = ""
for word in a.split():
   try:
      number = int(word)
      res += " | %d" % number
   except ValueError:
      res += " %s" % word

编辑:我试图提供“最简单”的解决方案。我的意思是,它更长,但我想更容易理解。不过,如果您了解其他解决方案(1行),请继续使用。

答案 3 :(得分:2)

使用正则表达式拆分re

import re


txt = 'Hans went to house number 10 92384 29349'

' | '.join(re.split('\s(?=\d)',txt))

# 'Hans went to house number | 10 | 92384 | 29349'

答案 4 :(得分:0)

以下是如何做到这一点:

a = 'Hans went to house number 10 92384 29349'

result = [' '.join([item for item in a.split(' ') if not item.isdigit()])] + [int(item) for item in a.split(' ') if item.isdigit()]

如果你想显示输出:

new_result = ' | '.join([str(item) for item in result])

答案 5 :(得分:0)

你可以这样做:

a = "Hans went to house number 10 92384 29349"

res = []

for item in a.split():
    if item.isdigit():
        res.extend(['|', item])
    else:
        res.append(item)

print(' '.join(res))
#Hans went to house number | 10 | 92384 | 29349