help on message splitting code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akilasekaran
    New Member
    • Feb 2012
    • 15

    help on message splitting code

    Code:
    filename = message.split()[1]  
    	f = open(filename[1:])  
            outputdata = f.read()
    what is the actual function of code here. first and second lines are hard to figure out :-/
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    To begin with, the indentation is wrong.

    message is a string object. String method split() is used to break the string into parts using whitespace as a delimiter.
    Code:
    >>> s = "A simple\tstring\nobject."
    >>> s
    'A simple\tstring\nobject.'
    >>> print s
    A simple	string
    object.
    >>> s.split()
    ['A', 'simple', 'string', 'object.']
    >>>
    The code expects element 1 ('simple' in the example above) to be the name of a file object with an unwanted prefix character.
    Code:
    >>> fn = s.split()[1]
    >>> fn[1:]
    'imple'
    >>>
    A file object is created with built-in function open() and read with file object method read().

    Really, this is very basic Python. All this information is readily available in the Python documentation. A good place to start is python.org.

    Comment

    • akilasekaran
      New Member
      • Feb 2012
      • 15

      #3
      i am familiar with split(),open() and read(). but my major concern is about the [1:].. i have neva come across such param with open(filename[1:]).

      1.could u temme what [1:] signifies?
      2.what is split function followed by [1] mean ?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by akilasekaran
        i am familiar with split(),open() and read(). but my major concern is about the [1:].. i have neva come across such param with open(filename[1:]).

        1.could u temme what [1:] signifies?
        2.what is split function followed by [1] mean ?
        1. Elements of sequence types are accessed by the index operator (s[i]), slice operator (s[i:j]) and extended slice operator (s[i:j:stride]). Given a string "abcdef":
        Code:
        >>> "abcdef"[1:]
        'bcdef'
        >>> "abcdef"[:1]
        'a'
        >>> "abcdef"[1]
        'b'
        >>> "abcdef"[0:5:2]
        'ace'
        >>>
        2. String method split() returns a list. Index operator s[1] returns the element at index 1. Note the first element in a list is at index 0.

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          Joining and splitting strings http://www.diveintopython.net/native...ing_lists.html and splitting and slicing http://www.freenetpages.co.uk/hp/alan.gauld/tuttext.htm.

          Comment

          • akilasekaran
            New Member
            • Feb 2012
            • 15

            #6
            oh wow i totally understood it.. thanks dwblas and bvdet :)

            Comment

            Working...