繁体   English   中英

如何打印字符串中的所有内容,直到 python 中的第一个数字?

[英]How would I print everything in a string up until the first number in python?

我有一个包含如下行的输入文件:

“堪萨斯城酋长队 42”

每行在单词和数字之间包含随机数量的空格。 我正在尝试确定一种可以分割两个值(单词部分和数字部分)的方法。 我理想的 output 将是:

“堪萨斯城酋长”

“42”

有任何想法吗?

签出这个正则表达式:

import re

your_string = "Kansas City Chiefs 42"
items = re.split(r'\s+(?=\d)|(?<=\d)\s+', your_string)

print(items)

你得到了:

['Kansas City Chiefs', '42']

如果您的要求是在获得第一个数字后立即阅读拆分,那么下面应该可以工作。

st = "Kansas City Chiefs 42"
text_part = ""

for each in st:
    if each.isnumeric():
        break
    text_part += each
number_part = st.replace(text_part, "")
print(text_part)
print(number_part)

您可以在任何一个值上使用 you.strip() ,具体取决于您是否要在末尾保留空格

这是我的实现:

from nltk import word_tokenize

sentence = "Kansas City Chiefs 42"
tokens = word_tokenize(sentence)
word_phrases = " ".join([token for token in tokens if not token.isnumeric()])
numeric_phrase = " ".join([token for token in tokens if token.isnumeric()])
print(word_phrases)
print(numeric_phrase)

回答

# Python3 program to extract all the numbers from a string 
import re 
  
# Function to extract all the numbers from the given string 
def getNumbers(str): 
    array = re.findall(r'[0-9]+', str) 
    return array 
  
# Driver code 
str = "adbv345hj43hvb42"
array = getNumbers(str) 
print(*array) 

Output:

345 43 42

您可以在 python 中使用正则表达式。

import re
def getNumber(str):
  arr = re.findall(r'[0-9]+', str)
str1 = "Kansas City Chiefs 42"
numbers = getNumber(str1)
str_val = str1[str1.find(numbers[0]):] # Kansas City Chiefs 
print(" ".join(numbers))
# output -> 42

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM