我有一个带有以下标题的静态CSV文档
City State Zip Latitude Longitude Subnet Wired1 Wired2 Wireless1 Wireless2 Domain Name NAT_DHCP NAT_Wireless
在搜索IP时,我会查看两个无线标头以及有线标头。
if (addr >= wired1 and addr <= wired2) or (addr >= wireless1 and addr <= wireless2):
print all header fields.
现在有些条目缺少无线范围和/或缺少有线范围。我收到以下错误:
netaddr.core.AddrFormatError: failed to detect a valid IP address from ''
更具体地说,在无线标题上:
File "script.py", line 175, in myfile
wireless1 = (int(IPAddress(row['Wireless1'])))
现在,我正在使用Dictreader,并且从文档中看,它是否应该跳过空行?有没有其他方法可以用来跳过线?
with open('csvfiles/myfile.csv', 'rb') as incsv:
reader = csv.DictReader(incsv, delimiter=',')
这是我的变量,如果好奇:
addr = (int(IPAddress(ip)))
wired1 = (int(IPAddress(row['Wired1'])))
wired2 = (int(IPAddress(row['Wired2'])))
wireless1 = (int(IPAddress(row['Wireless1'])))
wireless2 = (int(IPAddress(row['Wireless2'])))
示例csv行:
Wired1 Wired2 Wireless1 Wireless2
10.65.0.0 10.65.239.255 10.65.240.1 10.65.255.254
10.38.0.0 10.38.239.255
10.34.0.0 10.34.239.255 10.34.240.1 10.34.255.254
10.83.0.0 10.83.239.255
答案 0 :(得分:1)
考虑使用pandas
,您可以更好地操纵csv
文件。
import pandas as pd
df = pd.read_csv('your_file.csv')
#See how does your data looks like in pandas
print df
Wired1 Wired2 Wireless1 Wireless2
0 10.65.0.0 10.65.239.255 10.65.240.1 10.65.255.254
1 10.38.0.0 10.38.239.255 NaN NaN
2 10.34.0.0 10.34.239.255 10.34.240.1 10.34.255.254
3 10.83.0.0 10.83.239.255 NaN NaN
#Select only the rows where 'Wireless1' is not null
df[pd.notnull(df['Wireless1'])]
Wired1 Wired2 Wireless1 Wireless2
0 10.65.0.0 10.65.239.255 10.65.240.1 10.65.255.254
2 10.34.0.0 10.34.239.255 10.34.240.1 10.34.255.254
#Select only 'Wireless' clolumn where it is not null
df[pd.notnull(df['Wireless1'])]['Wireless1']
0 10.65.240.1
2 10.34.240.1
Name: Wireless1, dtype: object
同样,您可以使用pandas进行更多操作。因此,您可以避免将空值传递给netaddr
。