我一直在尝试创建一个 csv 文件,该文件将更改所有子网和 IP 地址范围,因此没有重叠。最初的问题在这里Editing csv file to edit subnets 因此 IP 范围没有重叠(Python 3.3 中的 ipaddress 模块),但我已经修复了它,所以if
语句停止了ValueError
s 并且第二个问题实际上不是问题。如果我可以重置迭代器,我相信我可以完成这项工作。它需要将 file1 中的每一行与 file2 中的每一行进行比较。
当我尝试这个时:
import ipaddress
import csv
from csv import DictReader, DictWriter
with open(r'file1.csv', newline='') as fin3,\
open(r'file2.csv', newline='') as fin4,\
open(r'file3.csv', 'w', newline='') as fout3:
read3 = DictReader(fin3) # fin 3 and 4 are copies of the same file
read4 = DictReader(fin4)
writenum3 = DictWriter(fout3, fieldnames=read3.fieldnames)
writenum3.writeheader()
for line3 in read3:
line3['Range']=ipaddress.ip_network(line3['Range']) # Convert IP ranges to ip network object
for line4 in read4:
line4['Range']=ipaddress.ip_network(line4['Range']) # Convert IP ranges to ip network object
if line3['Range'].netmask < line4['Range'].netmask: # To avoid "Not contained in" errors
if line3['Range'].overlaps(line4['Range']): # Tests for IP overlap
lst=list(line3['Range'].address_exclude(line4['Range'])) # List of subnets excluding line4 subnet
print (lst) # Temporary to view contents of lst
for val in lst:
line3['Range']=val # New range
line3[' IPStart']=val.network_address #New net address
line3[' IPStop']=val.broadcast_address #New broadcast address
writenum3.writerow(line3) # Write lines
它仅将 file1 的第一行(不是标题)与 file2 的第二行(不是标题)进行比较。
当我有这个(使用file.seek(0)
):
import ipaddress
import csv
from csv import DictReader, DictWriter
with open(r'file1.csv', newline='') as fin3,\
open(r'file2.csv', newline='') as fin4,\
open(r'file3.csv', 'w', newline='') as fout3:
read3 = DictReader(fin3) # fin 3 and 4 are copies of the same file
read4 = DictReader(fin4)
writenum3 = DictWriter(fout3, fieldnames=read3.fieldnames)
writenum3.writeheader()
for line3 in read3:
line3['Range']=ipaddress.ip_network(line3['Range']) # Convert IP ranges to ip network object
fin4.seek(0)
for line4 in read4:
line4['Range']=ipaddress.ip_network(line4['Range']) # Convert IP ranges to ip network object
if line3['Range'].netmask < line4['Range'].netmask: # To avoid "Not contained in" errors
if line3['Range'].overlaps(line4['Range']): # Tests for IP overlap
lst=list(line3['Range'].address_exclude(line4['Range'])) # List of subnets excluding line4 subnet
print (lst) # Temporary to view contents of lst
for val in lst:
line3['Range']=val # New range
line3[' IPStart']=val.network_address #New net address
line3[' IPStop']=val.broadcast_address #New broadcast address
writenum3.writerow(line3) # Write lines
它给出了一个错误ValueError: 'Range' does not appear to be an IPv4 or IPv6 network
。
如果我尝试该list()
方法(此处解释:Can iterators are reset in Python?):
import ipaddress
import csv
from csv import DictReader, DictWriter
with open(r'file1.csv', newline='') as fin3,\
open(r'file2.csv', newline='') as fin4,\
open(r'file3.csv', 'w', newline='') as fout3:
read3 = list(DictReader(fin3)) # fin 3 and 4 are copies of the same file
read4 = list(DictReader(fin4))
writenum3 = DictWriter(fout3, fieldnames=read3.fieldnames)
writenum3.writeheader()
for line3 in read3:
line3['Range']=ipaddress.ip_network(line3['Range']) # Convert IP ranges to ip network object
fin4.seek(0) # Reset iterator
for line4 in read4:
line4['Range']=ipaddress.ip_network(line4['Range']) # Convert IP ranges to ip network object
if line3['Range'].netmask < line4['Range'].netmask: # To avoid "Not contained in" errors
if line3['Range'].overlaps(line4['Range']): # Tests for IP overlap
lst=list(line3['Range'].address_exclude(line4['Range'])) # List of subnets excluding line4 subnet
print (lst) # Temporary to view contents of lst
for val in lst:
line3['Range']=val # New range
line3[' IPStart']=val.network_address #New net address
line3[' IPStop']=val.broadcast_address #New broadcast address
writenum3.writerow(line3) # Write lines
它给出了AttributeError: 'list' object has no attribute 'fieldnames'
错误。
我想知道我是否应该为此使用http://docs.python.org/3.4/library/itertools.html tee()
,但我不确定如何在这里使用它,或者它是否可以工作。
这是我的文件 1 和文件 2:
Zone Name, IPStart, IPStop,Range,Source
Group A,10.0.0.0,10.127.255.255,10.0.0.0/9,New List
Group A Sales,10.16.0.0,10.31.255.255,10.16.0.0/12,New List
Group A Marketing,10.62.0.0,10.62.255.255,10.62.0.0/16,New List
Group A Research,10.62.0.0,10.63.255.255,10.62.0.0/15,Old List
Group A Sales Primary routers,10.23.1.0,10.23.1.15,10.24.1.0/28,New List
Group A Sales Web Servers - Primary,10.18.0.0,10.18.0.255,10.18.0.0/24,New List
Group A Sales Web Servers,10.16.0.0,10.19.255.255,10.16.0.0/14,New List
Group B,10.128.0.0,10.255.255.255,10.128.0.0/9,Old List
这就是我最终想要的(尽管现在每个比较的所有子网都可以):
Zone Name, IPStart, IPStop,Range,Source
Group A,10.0.0.0,10.15.255.255,10.0.0.0/12,New List
Group A,10.32.0.0,10.47.255.255,10.32.0.0/12,New List
Group A,10.48.0.0,10.55.255.255,10.48.0.0/13,New List
Group A,10.56.0.0,10.59.255.255,10.56.0.0/14,New List
Group A,10.60.0.0,10.61.255.255,10.60.0.0/15,New List
Group A,10.64.0.0,10.127.255.255,10.64.0.0/10,New List
Group A Marketing,10.62.0.0,10.62.255.255,10.62.0.0/16,New List
Group A Research,10.63.0.0,10.63.255.255,10.63.0.0/16,Old List
Group A Sales,10.20.0.0,10.21.255.255,10.20.0.0/15,New List
Group A Sales,10.22.0.0,10.22.255.255,10.22.0.0/16,New List
Group A Sales,10.23.0.0,10.23.0.255,10.23.0.0/24,New List
Group A Sales,10.23.1.128,10.23.1.255,10.23.1.128/25,New List
Group A Sales,10.23.1.16,10.23.1.31,10.23.1.16/28,New List
Group A Sales,10.23.1.32,10.23.1.63,10.23.1.32/27,New List
Group A Sales,10.23.1.64,10.23.1.127,10.23.1.64/26,New List
Group A Sales,10.23.128.0,10.23.255.255,10.23.128.0/17,New List
Group A Sales,10.23.16.0,10.23.31.255,10.23.16.0/20,New List
Group A Sales,10.23.2.0,10.23.3.255,10.23.2.0/23,New List
Group A Sales,10.23.32.0,10.23.63.255,10.23.32.0/19,New List
Group A Sales,10.23.4.0,10.23.7.255,10.23.4.0/22,New List
Group A Sales,10.23.64.0,10.23.127.255,10.23.64.0/18,New List
Group A Sales,10.23.8.0,10.23.15.255,10.23.8.0/21,New List
Group A Sales,10.24.0.0,10.31.255.255,10.24.0.0/13,New List
Group A Sales,10.24.32.0,10.24.63.255,10.24.32.0/19,New List
Group A Sales Primary routers,10.23.1.0,10.23.1.15,10.24.1.0/28,New List
Group A Sales Web Servers,10.16.0.0,10.17.255.255,10.16.0.0/15,New List
Group A Sales Web Servers,10.19.0.0,10.19.255.255,10.19.0.0/16,New List
Group A Sales Web Servers,10.18.128.0,10.18.255.255,10.18.128.0/17,New List
Group A Sales Web Servers,10.18.64.0,10.18.127.255,10.18.64.0/18,New List
Group A Sales Web Servers,10.18.32.0,10.18.63.255,10.18.32.0/19,New List
Group A Sales Web Servers,10.18.16.0,10.18.31.255,10.18.16.0/20,New List
Group A Sales Web Servers,10.18.8.0,10.18.15.255,10.18.8.0/21,New List
Group A Sales Web Servers,10.18.4.0,10.18.7.255,10.18.4.0/22,New List
Group A Sales Web Servers,10.18.2.0,10.18.3.255,10.18.2.0/23,New List
Group A Sales Web Servers,10.18.1.0,10.18.1.255,10.18.1.0/24,New List
Group A Sales Web Servers - Primary,10.18.0.0,10.18.0.255,10.18.0.0/24,New List
Group B,10.128.0.0,10.255.255.255,10.128.0.0/9,Old List
任何帮助都值得赞赏,以使其按应有的方式工作。
谢谢, B0T