Re: [Tutor] Writing back to same CSV in the next column
On 23/08/15 14:16, Nym City wrote: Hello, Here is my final script. It is doing what I wanted it to. I wanted to just share it as a final product and thank you all for your feedback on the various previous revisions. import socket ListOfIPAddresses = [] with open('top500ips.csv', 'r') as f: for line in f: line = line.strip() ListOfIPAddresses.append(line) The initial assignment plus this for loop could all be replaced by readlines(): ListOfIPAddresses = f.readlines() newFile = open('top500ips.csv', 'w') You should use the 'with' style that you used above. Otherwise you should definitely close the file after the loop below. Do one or the other, but not both... for address in ListOfIPAddresses: try: ResolvedAddresses = socket.gethostbyaddr(address)[0] Being picky this variable should probably be named ResolvedAddress since its only one address being resolved at a time. newFile.write(ResolvedAddresses + "\n") # print(ResolvedAddresses) except socket.herror as e: newFile.write("No resolution available for %s" % (address) + "\n") Otherwise it looks fine to me. -- 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing back to same CSV in the next column
Hello, Here is my final script. It is doing what I wanted it to. I wanted to just share it as a final product and thank you all for your feedback on the various previous revisions. import socket ListOfIPAddresses = [] with open('top500ips.csv', 'r') as f: for line in f: line = line.strip() ListOfIPAddresses.append(line) newFile = open('top500ips.csv', 'w') for address in ListOfIPAddresses: try: ResolvedAddresses = socket.gethostbyaddr(address)[0] newFile.write(ResolvedAddresses + "\n") # print(ResolvedAddresses) except socket.herror as e: newFile.write("No resolution available for %s" % (address) + "\n") If you have any suggestions, please do share. Thank you. On Monday, August 17, 2015 4:35 AM, Alan Gauld wrote: 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing back to same CSV in the next column
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing back to same CSV in the next column
Hi, 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: try: name = socket.gethostbyaddr(line.strip()) ListOfIPAddresses.append(name) out_file.write(str(ListOfIPAddresses))[0] except socket.herror: out_file.write(line + '\t' + "No resolution available for ") in_file.close() out_file.close() - Also, could you please give some explanation of '\t'. Thanks. Thank you. On Sunday, August 16, 2015 5:58 PM, Alan Gauld 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 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing back to same CSV in the next column
Hello, Thank you for your guidance. Using your pseudocode I have put together the following: import socket import csv 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)) except socket.herror: out_file.write(line + '\t' + errrMsg) in_file.close() out_file.close() - I am getting few errors. Such as, I had to add 'str" in front of (name) to address TypeError: Can't convert 'int' object to str implicitly. Also, not sure why I keep getting SyntaxError: invalid syntax pointing to the except line. Please advise. Thank you. Thank you. On Wednesday, August 12, 2015 7:07 AM, Nym City via Tutor wrote: Hello, Please find the two requested files attached. The 'before' file is what I am reading into my program. The 'after' file is what I would like to have my output to look like. Ideally, I want it to be the same file but if its easier to create a new file for the output - that is ok too. I do not have the understanding of binary vs text modes. But I have found this online, can't say I understand it though: http://fileinfo.com/help/binary_vs_text_files Thank you. On Tuesday, August 11, 2015 4:10 AM, Alan Gauld wrote: On 11/08/15 01:23, Nym City via Tutor wrote: > import socket > import csv > > ListOfIPAddresses = [] > > with open('top500ips.csv', 'rb') as f: > for line in f: > line = line.strip() > ListOfIPAddresses.append(line) > f.close() You don;t need the f.close(). The 'with' structiure does that automatically. > # print(ListOfIPAddresses) > newFile = open('top500ips.csv', 'w') The original file was opened in binary mode, you are opening it here in text mode. Are you sure that's correct? Do you undertand the significance of binary v text modes? Also 'w' mode effectively creates a new empty file so you will need to recreate every line that was in the input file. Its usually better to rename the original file to something like top500ips.bak and then create a new file with the original name. If all goes well you can delete the .bak version, if something goes wrong you can rename it back to the original. > for address in ListOfIPAddresses: > try: > ResolvedAddresses = socket.gethostbyaddr(address)[0] You save the result into the variable but do nothing with it. The next time round the loop the result will be overwritten and the previous one lost. You are not writing anything to the file. > except socket.herror as e: > print("No resolution available for %s: %s" % (address, e)) > newFile.write.(ResolvedAddresses + "\n")Thank you. You are only writing to the file when you get the error. But at that point ResolvedAddresses will contain the result from the previous iteration of the loop so it may well be misleading. You in effect only write the host to file for the entry before lines that cause errors. I'm pretty sure thats not what you want. The other thing is that you are only writing the name. So your file will only contain a short column of names. Again I don't think that's what you wanted. Can you send us an example of before and after? ie about 5 lines of content from the file before you start and what it should look like after you finish? -- 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing back to same CSV in the next column
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 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing back to same CSV in the next column
Hello, Please find the two requested files attached. The 'before' file is what I am reading into my program. The 'after' file is what I would like to have my output to look like. Ideally, I want it to be the same file but if its easier to create a new file for the output - that is ok too. I do not have the understanding of binary vs text modes. But I have found this online, can't say I understand it though: http://fileinfo.com/help/binary_vs_text_files Thank you. On Tuesday, August 11, 2015 4:10 AM, Alan Gauld wrote: On 11/08/15 01:23, Nym City via Tutor wrote: > import socket > import csv > > ListOfIPAddresses = [] > > with open('top500ips.csv', 'rb') as f: > for line in f: > line = line.strip() > ListOfIPAddresses.append(line) > f.close() You don;t need the f.close(). The 'with' structiure does that automatically. > # print(ListOfIPAddresses) > newFile = open('top500ips.csv', 'w') The original file was opened in binary mode, you are opening it here in text mode. Are you sure that's correct? Do you undertand the significance of binary v text modes? Also 'w' mode effectively creates a new empty file so you will need to recreate every line that was in the input file. Its usually better to rename the original file to something like top500ips.bak and then create a new file with the original name. If all goes well you can delete the .bak version, if something goes wrong you can rename it back to the original. > for address in ListOfIPAddresses: > try: > ResolvedAddresses = socket.gethostbyaddr(address)[0] You save the result into the variable but do nothing with it. The next time round the loop the result will be overwritten and the previous one lost. You are not writing anything to the file. > except socket.herror as e: > print("No resolution available for %s: %s" % (address, e)) > newFile.write.(ResolvedAddresses + "\n")Thank you. You are only writing to the file when you get the error. But at that point ResolvedAddresses will contain the result from the previous iteration of the loop so it may well be misleading. You in effect only write the host to file for the entry before lines that cause errors. I'm pretty sure thats not what you want. The other thing is that you are only writing the name. So your file will only contain a short column of names. Again I don't think that's what you wanted. Can you send us an example of before and after? ie about 5 lines of content from the file before you start and what it should look like after you finish? -- 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing back to same CSV in the next column
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.1localhost 10.0.0.2remote host 10.0.0.3No resolution availabe for 10.0.0.3 10.0.0.4localhost 10.0.0.5No 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing back to same CSV in the next column
On 11/08/15 01:23, Nym City via Tutor wrote: import socket import csv ListOfIPAddresses = [] with open('top500ips.csv', 'rb') as f: for line in f: line = line.strip() ListOfIPAddresses.append(line) f.close() You don;t need the f.close(). The 'with' structiure does that automatically. # print(ListOfIPAddresses) newFile = open('top500ips.csv', 'w') The original file was opened in binary mode, you are opening it here in text mode. Are you sure that's correct? Do you undertand the significance of binary v text modes? Also 'w' mode effectively creates a new empty file so you will need to recreate every line that was in the input file. Its usually better to rename the original file to something like top500ips.bak and then create a new file with the original name. If all goes well you can delete the .bak version, if something goes wrong you can rename it back to the original. for address in ListOfIPAddresses: try: ResolvedAddresses = socket.gethostbyaddr(address)[0] You save the result into the variable but do nothing with it. The next time round the loop the result will be overwritten and the previous one lost. You are not writing anything to the file. except socket.herror as e: print("No resolution available for %s: %s" % (address, e)) newFile.write.(ResolvedAddresses + "\n")Thank you. You are only writing to the file when you get the error. But at that point ResolvedAddresses will contain the result from the previous iteration of the loop so it may well be misleading. You in effect only write the host to file for the entry before lines that cause errors. I'm pretty sure thats not what you want. The other thing is that you are only writing the name. So your file will only contain a short column of names. Again I don't think that's what you wanted. Can you send us an example of before and after? ie about 5 lines of content from the file before you start and what it should look like after you finish? -- 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing back to same CSV in the next column
Hello, Wanted to write back to see if anyone had the chance to review my previous request. import socket import csv ListOfIPAddresses = [] with open('top500ips.csv', 'rb') as f: for line in f: line = line.strip() ListOfIPAddresses.append(line) f.close() # print(ListOfIPAddresses) newFile = open('top500ips.csv', 'w') for address in ListOfIPAddresses: try: ResolvedAddresses = socket.gethostbyaddr(address)[0] except socket.herror as e: print("No resolution available for %s: %s" % (address, e)) newFile.write.(ResolvedAddresses + "\n")Thank you. On Sunday, August 2, 2015 7:47 PM, Nym City via Tutor wrote: Hello, Below is my program where I am reading a list of IPs from a CSV file and running it through the socket module. The result of the computation is stored in a variable named ResolvedAddresses. However, there are some that are not resolved and for those there is an exception. The task that I am trying to complete now is taking the output of the ResolvedAddresses and the content of the exception and write is back out to the same CSV file where the data is originally imported from. Ideally, I would like this new information to be in the column B and match with column A. Thank you in advance. And please let me know if what I am trying to accomplish is not clear and I'll try to explain it better. Here is the code: https://bpaste.net/show/01a412f72fa1 Thank you. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor