[Tutor] how to print array without adding newline

Dave Angel d at davea.name
Thu Sep 6 11:13:40 CEST 2012

On 08/18/2012 09:17 PM, vickistan wrote:
> Hello: I am trying to output an array to another program that takes an array
> as input, but the print statement adds a newline. If it were adding to each
> individual element, I could solve it easily, but it is adding one at the end
> of the array. Is there another way to print an array besides 
> print arrayname
> If it were a string, I have a whole host of options, but I need it to be
> output as an array. Each element is a url. I call it from a browser, and it
> works except for the added newline.
> Here are the relevant lines:
> =================
> /* code that connects to cloudfiles omitted */
> containers = conn.get_all_containers()
> i=0
> print "Content-type: text/html\n\n";
> wholelist=containers[0].list_objects()
> random.shuffle(wholelist)
> newlist=[]
> try:
>     del wholelist[int(sys.argv[1]):]
>     while i < int(sys.argv[1]):
>         newlist.append("http://example.com/"+wholelist[i].rstrip())
>         i = i+1
> except IndexError, e:
>     del newlist[5]
> print newlist
> ==============
> The output I am seeing is as follows:
> ['http://example.com/wet-longhaireddachshund.jpg',
> 'http://example.com/dachshund2.jpg',
> 'http://example.com/dachshundingrass.jpg'] 
> Any tips on better coding practices are welcome, but please don't beat me up
> Thanks,
> vickistan

I don't see any arrays in that code, just lists.  i also don't see how
that program could produce exactly that ouput, as it also prints

"Content-type: text/html\n\n";

But if you literally mean that only the final newline is a problem, then
just end the print statement with a comma:
    print newlist,

If you want more flexibility, instead of printing the list as a single
entity, you can just loop through it.  that way, you can choose which
newlines you want, if any.
    for item in newlist:
        print repr(item),    #or many other variants.  But you probably
want some delimeter at least.

it's not clear what your other program is expecting for stdin, since
there is no single standard for "takes an array for input."  it's also
unclear why a trailing linefeed should hurt you.  But I hope this will
help some.



More information about the Tutor mailing list