简体   繁体   English

从python中的字符串打印一个单词

[英]Print one word from a string in python

How can i print only certain words from a string in python ?如何仅从 python 中的字符串打印某些单词? lets say i want to print only the 3rd word (which is a number) and the 10th one假设我只想打印第 3 个单词(这是一个数字)和第 10 个单词

while the text length may be different each time而每次的文本长度可能不同

mystring = "You have 15 new messages and the size is 32000"

thanks.谢谢。

mystring = "You have 15 new messages and the size is 32000"
parts = mystring.split(' ')
message_count = int(parts[2])
message_size = int(parts[9])

It looks like you are matching something from program output or a log file.看起来您正在匹配程序输出或日志文件中的某些内容。

In this case you want to match enough so you have confidence you are matching the right thing, but not so much that if the output changes a little bit your program goes wrong.在这种情况下,您希望匹配足够多,以便您有信心匹配正确的东西,但不要太多以至于如果输出稍微改变,您的程序就会出错。

Regular expressions work well in this case, eg正则表达式在这种情况下效果很好,例如

>>> import re
>>> mystring = "You have 15 new messages and the size is 32000"
>>> match = re.search(r"(\d+).*?messages.*?size.*?(\d+)", mystring)
>>> if not match: print "log line didn't match"
... 
>>> messages, size = map(int, match.groups())
>>> messages
15
>>> size
32000
mystring = "You have 15 new messages and the size is 32000"
 
print mystring.split(" ")[2]  #Prints the 3rd word

print mystring.split(" ")[9] #Prints the 10th word

This function does the trick:这个函数可以解决问题:

def giveme(s, words=()):
    lista = s.split()    
    return [lista[item-1] for item in words]   

mystring = "You have 15 new messages and the size is 32000"
position = (3, 10)
print giveme(mystring, position)

it prints -> ['15', '32000']

The alternative indicated by Ignacio is very clean: Ignacio 指出的替代方案非常干净:

import operator

mystring = "You have 15 new messages and the size is 32000"
position = (2, 9)

lista = mystring.split()
f = operator.itemgetter(*position)
print f(lista)

it prints -> ['15', '32000']

operator.itemgetter() ... operator.itemgetter() ...

Return a callable object that fetches the given item(s) from its operand.返回一个可调用对象,该对象从其操作数中获取给定项。

After, f = itemgetter(2) , the call f(r) returns r[2].f = itemgetter(2) ,调用f(r)返回 r[2]。

After, g = itemgetter(2,5,3) , the call g(r) returns (r[2], r[5], r[3])之后, g = itemgetter(2,5,3) ,调用g(r)返回 (r[2], r[5], r[3])

Note that now positions in position should be counted from 0 to allow using directly the *position argument请注意,现在位置位置应该从 0 开始计数,以允许直接使用 *position 参数

Take a look at str.split() .看看str.split()

Alternatively, if you're looking for certain things you might try regular expressions instead;或者,如果您正在寻找某些东西,您可以尝试使用正则表达式; that could cope with the filler words changing, even.甚至可以应对填充词的变化。 But if all you care about is word position within the string, then splitting and printing out certain elements of the resulting list would be the most straightforward.但是,如果您只关心字符串中的单词位置,那么拆分并打印出结果列表的某些元素将是最直接的。

How about this one:这个怎么样:

import re

tst_str = "You have 15 new messages and the size is 32000"
items = re.findall(r" *\d+ *",tst_str)
for item in items:
    print(item)

Result:结果:

 15 
 32000

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

相关问题 如何在Python中一次打印一个单词的字符串? - How do I print a string one word at a time in Python? 以单词格式打印python列表,例如一个字符串 - Print a python list in a word format like is one string 如何在Python中使用for循环从字符串中打印每个唯一单词的频率 - How to print frequency of each unique word from a string with for loop in python Python不会从字符串打印唯一单词的多个位置 - Python does not print unique word's multiple positions from string 如何在一行显示所有字符串的情况下,一次一行打印一个单词。 -Python - How to print a string one word at a time, on one line with all of the string visible. - Python 从字符串中提取并打印一个单词 - Extract from string and print a word 如何从 python 中的用户那里获取一个字符串(一个单词)? - How to take one string (one word) from user in python? Python if len(“string”) &lt;= 1:如何打印单词? @codeacademy - Python if len(“string”) <= 1: how to print the word? @codeacademy 从列表中的一个字符串中搜索任何单词或单词组合(python) - Search for any word or combination of words from one string in a list (python) 查找字符串是否包含列表中的单词之一并打印它找到的单词 - Find if string contains one of the word in list and print the word it finds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM