#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep Power
    0

    Make a list into one big string


    Hi, I'm encountering a problem where I capture an output from a device, but it seems as every word is an item on a list.
    This is my capture out put.
    Code:
    print results
    
    !
    interface GigabitEthernet0/1
     description Reserved for Infrastructure
     switchport access vlan 101
     switchport mode access
     spanning-tree portfast
    end
    print results[0]
    s
    print results[1]
    h
    As you can see it look like that each word is item on a list, but if I type print results[0], I would expect ! to print not the s. I'm a little confude about that. But my end result is that the output is just one string. Thanks
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,892
    Rep Power
    481
    I cannot tell much from your post. Do this:

    print(type(results))



    When you discover that results is not type(str()) investigate the type that it is.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep Power
    0
    This is what I get:

    print (type(results))
    TypeError: 'str' object is not callable
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,892
    Rep Power
    481
    Great. I don't write code that overwrites my system's available names. You seem to have a code that violates my rules.

    Try

    print(__builtins__.type(results))
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Great. I don't write code that overwrites my system's available names. You seem to have a code that violates my rules.

    Try

    print(__builtins__.type(results))
    The result was one big string already. Thanks
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,892
    Rep Power
    481
    Look,
    Code:
    >>> def f(o):print __builtins__.type(o)
    ... 
    >>> f(3)
    <type 'int'>
    >>> f('string')
    <type 'str'>
    >>> f(object)
    <type 'type'>
    >>> f(object())
    <type 'object'>
    >>>
    It's not "one big string".

    Furthermore, the substring "sh" given by your results[0]+results[1] does not appear in your first post.

    Something screwy's happening and we're both clueless. Were we given complete information we might untangle things.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0
    The only way I can think to do this would be something like this.

    Code:
    list = [1,2,3]
    string = ""
    for x in list:
      string += x
    print string
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    438
    Rep Power
    67
    Originally Posted by amb1s1
    As you can see it look like that each word is item on a list,
    It most definitely does NOT look like that. It looks like a string with line terminators in it, as for example in:

    Code:
    >>> results = '''
    !
    interface GigabitEthernet0/1
     description Reserved for Infrastructure
     switchport access vlan 101
     switchport mode access
     spanning-tree portfast
    end'''
    >>> print(results)
    
    !
    interface GigabitEthernet0/1
     description Reserved for Infrastructure
     switchport access vlan 101
     switchport mode access
     spanning-tree portfast
    end
    >>> print(results[0])
    
    
    >>> print(results[1])
    !
    But as b49P23TIvg already pointed out, its hard to see why results[0] could be s and results[1] h.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)

IMN logo majestic logo threadwatch logo seochat tools logo