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

Nym City nymcity at yahoo.com
Mon Aug 17 03:51:43 CEST 2015

Thank you for your response. I fixed the parenthesis and it worked.

I made little modification below. By default 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. I looked up the error but most examples that I found were not applicable to my code purpose. 
import socket

in_file = open('top500ips.csv', 'r')
out_file = open('top500ips_out.csv', 'w')

ListOfIPAddresses = []

for line in in_file:
        name = socket.gethostbyaddr(line.strip())
    except socket.herror:
        out_file.write(line + '\t' + "No resolution available for ")

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


  Thank you. 

     On Sunday, August 16, 2015 5:58 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:

 On 16/08/15 22:42, Nym City wrote:
> import socket
> import csv

You don't need csv, you aren't using it.

> in_file = open('top500ips.csv', 'r')
> out_file = open('top500ips_out.csv', 'w')
> for line in in_file:
>    try:
>        name = socket.gethostbyaddr(line.strip())
>        out_file.write(line + '\t' + (str(name))

count the parens in the line above...

>    except socket.herror:
>        out_file.write(line + '\t' + errrMsg)
> in_file.close()
> out_file.close()

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:


More information about the Tutor mailing list