The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Make a list into one big string
Discuss Make a list into one big string in the Python Programming forum on Dev Shed. Make a list into one big string Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 22nd, 2012, 10:44 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 25
Time spent in forums: 4 h 25 m 28 sec
Reputation 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
|

August 22nd, 2012, 10:56 AM
|
 |
Contributing User
|
|
|
|
|
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!
|

August 22nd, 2012, 11:00 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 25
Time spent in forums: 4 h 25 m 28 sec
Reputation Power: 0
|
|
|
This is what I get:
print (type(results))
TypeError: 'str' object is not callable
|

August 22nd, 2012, 11:18 AM
|
 |
Contributing User
|
|
|
|
|
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))
|

August 22nd, 2012, 02:37 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 25
Time spent in forums: 4 h 25 m 28 sec
Reputation Power: 0
|
|
Quote: | 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
|

August 22nd, 2012, 02:53 PM
|
 |
Contributing User
|
|
|
|
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.
|

September 3rd, 2012, 05:43 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
Time spent in forums: 19 m 49 sec
Reputation 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
|

September 4th, 2012, 03:57 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | 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, it’s hard to see why results[0] could be “s” and results[1] “h”.
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|