[英]Returning multiple values to a dataframe from a for loop
import pandas as pd
def makeaddr(a,b,c,d):
return ipaddress.IPv4Address(bytes([a,b,c,d]))
def ipaddr():
for x in range(256):
a = makeaddr(x,x,x,x)
a = str(a)
return a
data = {'Ipaddress':[ipaddr() for x in range(256)]}
df = pd.DataFrame({key:pd.Series(value) for key, value in data.items() })
print(df)
如何將我的代碼中的ipaddress從0.0.0.0返回到xxxx到字典中,以便我的數據框可以打印ipaddress。
我正計划制作一個數據框,該數據框將給我輸出
我希望我的程序打印出從0.0.0.0到255.255.255.255的所有IP地址,但是要做到這一點,我需要ipaddr()返回多個值,有沒有辦法做到這一點?
我找到了解決我的問題的方法,如下所示:
import pandas as pd
import ipaddress
import socket
import geocoder
import threading
import csv
list1= []
list2= []
list3= []
list4= []
def makeaddr(a,b,c,d):
return ipaddress.IPv4Address(bytes([a,b,c,d]))
def ip():
for x in range(256):
try:
a = makeaddr(0,0,0,x)
b = makeaddr(0,0,0,x)
b = str(b)
hostb = socket.gethostbyaddr(str(b))
hostname = hostb[0]
g = geocoder.ip(b)
g1 = g.latlng
g2 = g.city
list1.append(a)
list2.append(hostname)
list3.append(g1)
list4.append(g2)
except:
list1.append(a)
def ip1():
for x in range(256):
try:
a = makeaddr(0,0,x,255)
b = makeaddr(0,0,x,255)
b = str(b)
hostb = socket.gethostbyaddr(str(b))
hostname = hostb[0]
g = geocoder.ip(b)
g1 = g.latlng
g2 = g.city
list1.append(a)
list2.append(hostname)
list3.append(g1)
list4.append(g2)
except:
list1.append(a)
def ip2():
for x in range(256):
try:
a = makeaddr(0,x,255,255)
b = makeaddr(0,x,255,255)
b = str(b)
hostb = socket.gethostbyaddr(str(b))
hostname = hostb[0]
g = geocoder.ip(b)
g1 = g.latlng
g2 = g.city
list1.append(a)
list2.append(hostname)
list3.append(g1)
list4.append(g2)
except:
list1.append(a)
def ip3():
for x in range(256):
try:
a = makeaddr(x,255,255,255)
b = makeaddr(x,255,255,255)
b = str(b)
c = 'Host Not Found'
hostb = socket.gethostbyaddr(str(b))
hostname = hostb[0]
g = geocoder.ip(b)
g1 = g.latlng
g2 = g.city
list1.append(a)
list2.append(hostname)
list3.append(g1)
list4.append(g2)
except:
list1.append(a)
threads = []
t1 = threading.Thread(target=ip(),args=())
t2 = threading.Thread(target=ip1(),args=())
t3 = threading.Thread(target=ip2(),args=())
t4 = threading.Thread(target=ip3(),args=())
t1.start()
t2.start()
t3.start()
t4.start()
data = {'IP':list1,'L&L':list3,'City':list4,'DN':list2}
df = pd.DataFrame({key:pd.Series(value) for key, value in data.items() }) #using a dict
print(df)
df.to_csv('csv12345.csv', sep ='\t')
如果有人可以減少LOC或改進此代碼,請隨時執行。
import pandas as pd
import random
import ipaddress
def makeaddr(ip):
return ipaddress.IPv4Address(bytes(ip))
我將對您的ipaddr()
函數進行一些更改,因為此刻,它返回的是標量而不是列表。
def ipaddr():
four_random_ints = [random.randint(0, 256) for x in range(4)]
address = makeaddr(four_random_ints)
return address
names = ['Tom', 'Jack', 'Steve', 'Ricky']
data = {'Names': names,
'IP address':[ipaddr() for name in names]}
輸出量
{'Names': ['Tom', 'Jack', 'Steve', 'Ricky'],
'IP address': [IPv4Address('43.70.58.224'),
IPv4Address('71.142.187.150'),
IPv4Address('39.224.203.109'),
IPv4Address('96.242.248.198')]}
df = pd.DataFrame(data)
輸出量
Names IP address
0 Tom 43.70.58.224
1 Jack 71.142.187.150
2 Steve 39.224.203.109
3 Ricky 96.242.248.198
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.