Find an Item in List , Export it to Excel in Python [closed]
I have couple of Ip's listed in output.txt file . i want to resolve those IP's to NAME through "nslookup" and then export the result to an excel.i have the below code written which does the "nslookup" job .
i want to pick the "Name " and "Address" of each list ( nslookup result for each IP becomes one list ) through my code and write that in excel in two different column..Any idea how to do ?
import subprocess
import xlwt
from tempfile import TemporaryFile
ip =
f = open("output.txt")
for line in f:
ip.append(line)
f.close()
for i in ip:
ip_arr =
process = subprocess.Popen(["nslookup", i], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.communicate()[0].split('n')
print output
print type(output)
for data in output:
#ip_arr =
if 'Name' in data:
ip_arr.append(data.replace('r',''))
if 'Address' in data:
ip_arr.append(data.replace('r',''))
ip_arr.pop(0)
print ip_arr
book = xlwt.Workbook()
sheet1 = book.add_sheet('sheet1')
for i,e in enumerate(ip_arr):
sheet1.write(i,1,e)
name = "random.xls"
book.save(name)
book.save(TemporaryFile())
output.txt:
4.2.2.2
8.8.8.8
9.9.9.9
excel python-2.7 list
closed as unclear what you're asking by jonrsharpe, EdChum, jpp, grizzthedj, Mickael Maison Dec 31 '18 at 16:34
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have couple of Ip's listed in output.txt file . i want to resolve those IP's to NAME through "nslookup" and then export the result to an excel.i have the below code written which does the "nslookup" job .
i want to pick the "Name " and "Address" of each list ( nslookup result for each IP becomes one list ) through my code and write that in excel in two different column..Any idea how to do ?
import subprocess
import xlwt
from tempfile import TemporaryFile
ip =
f = open("output.txt")
for line in f:
ip.append(line)
f.close()
for i in ip:
ip_arr =
process = subprocess.Popen(["nslookup", i], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.communicate()[0].split('n')
print output
print type(output)
for data in output:
#ip_arr =
if 'Name' in data:
ip_arr.append(data.replace('r',''))
if 'Address' in data:
ip_arr.append(data.replace('r',''))
ip_arr.pop(0)
print ip_arr
book = xlwt.Workbook()
sheet1 = book.add_sheet('sheet1')
for i,e in enumerate(ip_arr):
sheet1.write(i,1,e)
name = "random.xls"
book.save(name)
book.save(TemporaryFile())
output.txt:
4.2.2.2
8.8.8.8
9.9.9.9
excel python-2.7 list
closed as unclear what you're asking by jonrsharpe, EdChum, jpp, grizzthedj, Mickael Maison Dec 31 '18 at 16:34
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
You are resetting the list in the loop with this lineip_arr =which is why you only get the last entry.
– Burhan Khalid
Dec 31 '18 at 11:13
@Burhan Khalid : i removed that , now i am getting everything but repeated twice...
– Jinni
Dec 31 '18 at 11:35
That's because you are adding it twice (you have twoip_arr.appendstatements).
– Burhan Khalid
Dec 31 '18 at 11:37
@BurhanKhalid hmm..how it should be ?
– Jinni
Dec 31 '18 at 11:42
add a comment |
I have couple of Ip's listed in output.txt file . i want to resolve those IP's to NAME through "nslookup" and then export the result to an excel.i have the below code written which does the "nslookup" job .
i want to pick the "Name " and "Address" of each list ( nslookup result for each IP becomes one list ) through my code and write that in excel in two different column..Any idea how to do ?
import subprocess
import xlwt
from tempfile import TemporaryFile
ip =
f = open("output.txt")
for line in f:
ip.append(line)
f.close()
for i in ip:
ip_arr =
process = subprocess.Popen(["nslookup", i], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.communicate()[0].split('n')
print output
print type(output)
for data in output:
#ip_arr =
if 'Name' in data:
ip_arr.append(data.replace('r',''))
if 'Address' in data:
ip_arr.append(data.replace('r',''))
ip_arr.pop(0)
print ip_arr
book = xlwt.Workbook()
sheet1 = book.add_sheet('sheet1')
for i,e in enumerate(ip_arr):
sheet1.write(i,1,e)
name = "random.xls"
book.save(name)
book.save(TemporaryFile())
output.txt:
4.2.2.2
8.8.8.8
9.9.9.9
excel python-2.7 list
I have couple of Ip's listed in output.txt file . i want to resolve those IP's to NAME through "nslookup" and then export the result to an excel.i have the below code written which does the "nslookup" job .
i want to pick the "Name " and "Address" of each list ( nslookup result for each IP becomes one list ) through my code and write that in excel in two different column..Any idea how to do ?
import subprocess
import xlwt
from tempfile import TemporaryFile
ip =
f = open("output.txt")
for line in f:
ip.append(line)
f.close()
for i in ip:
ip_arr =
process = subprocess.Popen(["nslookup", i], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.communicate()[0].split('n')
print output
print type(output)
for data in output:
#ip_arr =
if 'Name' in data:
ip_arr.append(data.replace('r',''))
if 'Address' in data:
ip_arr.append(data.replace('r',''))
ip_arr.pop(0)
print ip_arr
book = xlwt.Workbook()
sheet1 = book.add_sheet('sheet1')
for i,e in enumerate(ip_arr):
sheet1.write(i,1,e)
name = "random.xls"
book.save(name)
book.save(TemporaryFile())
output.txt:
4.2.2.2
8.8.8.8
9.9.9.9
excel python-2.7 list
excel python-2.7 list
edited Jan 1 at 9:17
Jinni
asked Dec 31 '18 at 11:00
JinniJinni
166
166
closed as unclear what you're asking by jonrsharpe, EdChum, jpp, grizzthedj, Mickael Maison Dec 31 '18 at 16:34
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by jonrsharpe, EdChum, jpp, grizzthedj, Mickael Maison Dec 31 '18 at 16:34
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
You are resetting the list in the loop with this lineip_arr =which is why you only get the last entry.
– Burhan Khalid
Dec 31 '18 at 11:13
@Burhan Khalid : i removed that , now i am getting everything but repeated twice...
– Jinni
Dec 31 '18 at 11:35
That's because you are adding it twice (you have twoip_arr.appendstatements).
– Burhan Khalid
Dec 31 '18 at 11:37
@BurhanKhalid hmm..how it should be ?
– Jinni
Dec 31 '18 at 11:42
add a comment |
You are resetting the list in the loop with this lineip_arr =which is why you only get the last entry.
– Burhan Khalid
Dec 31 '18 at 11:13
@Burhan Khalid : i removed that , now i am getting everything but repeated twice...
– Jinni
Dec 31 '18 at 11:35
That's because you are adding it twice (you have twoip_arr.appendstatements).
– Burhan Khalid
Dec 31 '18 at 11:37
@BurhanKhalid hmm..how it should be ?
– Jinni
Dec 31 '18 at 11:42
You are resetting the list in the loop with this line
ip_arr = which is why you only get the last entry.– Burhan Khalid
Dec 31 '18 at 11:13
You are resetting the list in the loop with this line
ip_arr = which is why you only get the last entry.– Burhan Khalid
Dec 31 '18 at 11:13
@Burhan Khalid : i removed that , now i am getting everything but repeated twice...
– Jinni
Dec 31 '18 at 11:35
@Burhan Khalid : i removed that , now i am getting everything but repeated twice...
– Jinni
Dec 31 '18 at 11:35
That's because you are adding it twice (you have two
ip_arr.append statements).– Burhan Khalid
Dec 31 '18 at 11:37
That's because you are adding it twice (you have two
ip_arr.append statements).– Burhan Khalid
Dec 31 '18 at 11:37
@BurhanKhalid hmm..how it should be ?
– Jinni
Dec 31 '18 at 11:42
@BurhanKhalid hmm..how it should be ?
– Jinni
Dec 31 '18 at 11:42
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are resetting the list in the loop with this line
ip_arr =which is why you only get the last entry.– Burhan Khalid
Dec 31 '18 at 11:13
@Burhan Khalid : i removed that , now i am getting everything but repeated twice...
– Jinni
Dec 31 '18 at 11:35
That's because you are adding it twice (you have two
ip_arr.appendstatements).– Burhan Khalid
Dec 31 '18 at 11:37
@BurhanKhalid hmm..how it should be ?
– Jinni
Dec 31 '18 at 11:42