Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Line breaks in list causing a small formatting problem while joiningthe list (http://www.velocityreviews.com/forums/t742383-line-breaks-in-list-causing-a-small-formatting-problem-while-joiningthe-list.html)

Oltmans 01-21-2011 03:39 PM

Line breaks in list causing a small formatting problem while joiningthe list
 
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.

Peter Otten 01-21-2011 04:25 PM

Re: Line breaks in list causing a small formatting problem while joining the list
 
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 |


MRAB 01-21-2011 06:54 PM

Re: Line breaks in list causing a small formatting problem whilejoining the list
 
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")


All times are GMT. The time now is 08:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.