[英]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)
# 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.