Print one word from a string in python

前端 未结 6 884
既然无缘
既然无缘 2021-01-14 01:21

How can i print only certain words from a string in python ? lets say i want to print only the 3rd word (which is a number) and the 10th one

while the text length ma

相关标签:
6条回答
  • 2021-01-14 01:31
    mystring = "You have 15 new messages and the size is 32000"
    parts = mystring.split(' ')
    message_count = int(parts[2])
    message_size = int(parts[9])
    
    0 讨论(0)
  • 2021-01-14 01:37

    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:

    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() ...

    Return a callable object that fetches the given item(s) from its operand.

    After, f = itemgetter(2), the call f(r) returns r[2].

    After, g = itemgetter(2,5,3), the call g(r) returns (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 讨论(0)
  • 2021-01-14 01:37

    Take a look at 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.

    0 讨论(0)
  • 2021-01-14 01:40

    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
    
    0 讨论(0)
  • 2021-01-14 01:51

    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
    
    0 讨论(0)
  • 2021-01-14 01:54
    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
    
    0 讨论(0)
提交回复
热议问题