Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Line breaks in list causing a small formatting problem while joiningthe list

Reply
Thread Tools

Line breaks in list causing a small formatting problem while joiningthe list

 
 
Oltmans
Guest
Posts: n/a
 
      01-21-2011
Hi Python gurus, hope you're doing well. I've a small problem.

When I run the following code
__________________________________________________ _
>>> names = ['oltmans','abramhovic','\n','sal','lee']
>>> print '| ' + ' | '.join(names)

| oltmans | abramhovic |
| sal | lee
__________________________________________________ _

I get the output like above. However, I want it to output like below

| oltmans | abramhovic |
| sal | lee


That is, there shouldn't be a space in the beginning of second line.
The list can of course contain more than 5 elements. Any ideas? I will
appreciate any hint. Thanks in advance.
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-21-2011
Oltmans wrote:

> Hi Python gurus, hope you're doing well. I've a small problem.
>
> When I run the following code
> __________________________________________________ _
>>>> names = ['oltmans','abramhovic','\n','sal','lee']
>>>> print '| ' + ' | '.join(names)

> | oltmans | abramhovic |
> | sal | lee
> __________________________________________________ _
>
> I get the output like above. However, I want it to output like below
>
> | oltmans | abramhovic |
> | sal | lee
>
>
> That is, there shouldn't be a space in the beginning of second line.
> The list can of course contain more than 5 elements. Any ideas? I will
> appreciate any hint. Thanks in advance.


>>> print "|%s|" % "|".join(n if n == "\n" else " %s " % n for n in names)

| oltmans | abramhovic |
| sal | lee |

 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      01-21-2011
On 21/01/2011 16:25, Peter Otten wrote:
> Oltmans wrote:
>
>> Hi Python gurus, hope you're doing well. I've a small problem.
>>
>> When I run the following code
>> __________________________________________________ _
>>>>> names = ['oltmans','abramhovic','\n','sal','lee']
>>>>> print '| ' + ' | '.join(names)

>> | oltmans | abramhovic |
>> | sal | lee
>> __________________________________________________ _
>>
>> I get the output like above. However, I want it to output like below
>>
>> | oltmans | abramhovic |
>> | sal | lee
>>
>>
>> That is, there shouldn't be a space in the beginning of second line.
>> The list can of course contain more than 5 elements. Any ideas? I will
>> appreciate any hint. Thanks in advance.

>
>>>> print "|%s|" % "|".join(n if n == "\n" else " %s " % n for n in names)

> | oltmans | abramhovic |
> | sal | lee |
>

Or:

print ('| ' + ' | '.join(names)).replace("\n ", "\n")
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with line breaks ATR2000 ASP .Net 1 09-20-2008 10:08 AM
Small breaks seweryn Cisco 1 12-17-2006 10:44 PM
Beginner: read $array with line breaks line by line Marek Stepanek Perl Misc 12 09-02-2006 10:27 AM
Force multi line field value to output with line breaks? bernadou ASP .Net Web Controls 2 01-23-2006 01:23 PM
Re: javascript causing line breaks PJ ASP .Net 1 08-04-2003 12:56 AM



Advertisments