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

Alan Gauld alan.gauld at btinternet.com
Wed Aug 12 03:16:30 CEST 2015


On 12/08/15 01:58, Nym City wrote:

 > Please find the two requested files attached.

Since this is a text mailing list it's better to insert the files into
the email. Many servers will strip out attachments so they
can't be seen.

I've pasted them below.

Before:
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.5

After:
10.0.0.1    localhost
10.0.0.2    remote host
10.0.0.3    No resolution availabe for 10.0.0.3
10.0.0.4    localhost
10.0.0.5    No resolution availabe for 10.0.0.5

So, basically you just want to append the name (or an error message)?

In pseudocode this would be

open in_file
open out_file

for line in in_file:
      try:
          name = gethostaddr(line.strip())
          out_file.write(line + '\t' + name)
      except herror:
          out_file.write(line + '\t' + errrMsg)
close in_file
close out_file

delete in_file   # or rename to .bak
rename out_file to in_file


Can you translate that to Python?

-- 
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