[Tutor] Writing back to same CSV in the next column

Alan Gauld alan.gauld at btinternet.com
Mon Aug 17 10:35:13 CEST 2015


On 17/08/15 02:51, Nym City via Tutor wrote:
 > the output of the gethostbyaddr module includes three item
> (hostname, aliaslist, ipaddrlist). However, in my output
> I just what the hostname field. So I created a list but
> I am not able to pull out just the [0] item from this
 > and instead I get the following error:
 > TypeError: 'int' object is not subscriptable.

> for line in in_file:
>      try:
>          name = socket.gethostbyaddr(line.strip())
>          ListOfIPAddresses.append(name)
>          out_file.write(str(ListOfIPAddresses))[0]

Look at that last line and break it down.
Where is the index operation?

It's right at the end so it applies to the output
of the write() operation. You need it against
the list, before you convert to string and before
you write to file.

> Also, could you please give some explanation of '\t'.

Its the tab character. it inserts a tab, just like hitting
the tab key on the keyboard.
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list