[英]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 callf(r)
returns r[2].在f = itemgetter(2)
,调用f(r)
返回 r[2]。After,
g = itemgetter(2,5,3)
, the callg(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.