[Tutor] how to print array without adding newline

vicki at thepenguin.org vicki at thepenguin.org
Thu Sep 6 15:56:38 CEST 2012


Thank you for your reply. I understand that it is odd, but my program is being called from a hubot and returning data to it as well. I have figured out how to make the changes to get it to output the correct data in the correct format, but now I am getting a "Premature end of script headers" error. I have the correct #! line and the output from the command line shows no errors that would be interfering. Is there a way to make sure it is showing me all the errors? To increase error logging?
------
!/usr/bin/env python
import cloudfiles
import random
import sys
import array

conn = cloudfiles.get_connection('username', 'key')

containers = conn.get_all_containers()
i=0
print "Content-type: text/html";
wholelist=containers[0].list_objects()
random.shuffle(wholelist)
newlist=[]
#newlist=wholelist[:]
try:
#    print sys.argv[1]
    if "=" in sys.argv[1]: sys.argv[1] = sys.argv[1].rstrip("=")
#    print sys.argv[1]
    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]
except Exception, err:
    print 'Caught an exception'
print newlist,
-------
Vicki

>  -------Original Message-------
>  From: Dave Angel <d at davea.name>
>  To: vickistan <vicki at stanfield.net>
>  Cc: tutor at python.org
>  Subject: Re: [Tutor] how to print array without adding newline
>  Sent: Sep 06 '12 05:13
>  
>  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.
>  
>  
>  --
>  
>  DaveA
>  
>  


More information about the Tutor mailing list