简体   繁体   English

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

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

I have an input file containing lines like the following:我有一个包含如下行的输入文件:

"Kansas City Chiefs 42" “堪萨斯城酋长队 42”

Each line contains a random number of spaces between the words and the numbers.每行在单词和数字之间包含随机数量的空格。 I am trying to identify a way that I can slice the two values (word portion and number portion).我正在尝试确定一种可以分割两个值(单词部分和数字部分)的方法。 My ideal output would be:我理想的 output 将是:

"Kansas City Chiefs" “堪萨斯城酋长”

"42" “42”

Any ideas?有任何想法吗?

Checkout this regex:签出这个正则表达式:

import re

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

print(items)

you got:你得到了:

['Kansas City Chiefs', '42']

If your requirement is to read split as soon as you get the first number then below should work.如果您的要求是在获得第一个数字后立即阅读拆分,那么下面应该可以工作。

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 can you.strip() on either of the values depending on whether you want to keep the spaces at the end or not您可以在任何一个值上使用 you.strip() ,具体取决于您是否要在末尾保留空格

Here's my implementation:这是我的实现:

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)

Answer 回答

# 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: Output:

345 43 42

You can use regex in python.您可以在 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.

相关问题 我如何在 a 之前获得所有内容:在字符串 Python - How would I get everything before a : in a string Python Python正则表达式获取所有内容直到字符串中的第一个点 - Python regex to get everything until the first dot in a string 如何删除字符串中的所有内容,直到在 Python 中看到一个或多个字符 - How can I remove everything in a string until a character(s) are seen in Python Python正在打印所有内容,我只希望它打印最后一个数字 - Python is printing everything, I only want it to print the last number 如何从一个条件打印字符串,直到满足另一个条件? - How can I print a string from one criteria, up until another criteria is met? 删除所有内容,直到python中的某些字符串或字符 - Delete everything until certain string or character in python 匹配所有内容,直到可选字符串(Python正则表达式) - Match everything until optional string (Python regex) 如何在数据框中找到每行最长的字符串并在超过一定数量时打印行号 - How would I find the longest string per row in a data frame and print the row number if it exceeds a certain amount 我怎么会打印python文档字符串? - How would I pretty print a python docstring? 我如何在 python 中将任何数字 x 次打印为列表? - How would I print out any number x times as a list in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM