python循环判断问题
filepath=r'C:\HOSTCONINFO.csv'
linuxfilepath=r'C:\11.csv'
linuxfilepathnew=r'C:\index_new.csv'
IPList={}
with open(filepath,'r',encoding='UTF-8') as f:
title=f.readline()
line=f.readline()
while line:
IPList[line.split(',')[3]]=','.join(line.split(',')[4:8])
line=f.readline()
print(IPList)
with open(linuxfilepath, 'r', encoding='UTF-8') as flinux:
with open(linuxfilepathnew, 'w', encoding='UTF-8') as flinuxnew:
# line = flinux.readline()#剔除空行
title=flinux.readline()
flinuxnew.write(title.split('\n')[0]+',项目,负责人,电话,组织\n')
line = flinux.readline()
line = line.split('\n')[0]
while line:
print(line.split(',')[0])
try:
print(IPList[line.split(',')[0]])
if IPList[line.split(',')[0]] !=None:
if ';' in line.split(',')[0]:
for data in line.split(',')[0].split(';'):
try:
if IPList[line.split(',')[0]] !=None:
flinuxnew.write(line.split('\n')[0] + ',' + IPList[line.split(',')[0]] + '\n')
break
else:
continue
except Exception as err:
print(str(err))
continue
else:
flinuxnew.write(line.split('\n')[0]+','+IPList[line.split(',')[0]]+'\n')
else:
flinuxnew.write(line)
except Exception as e:
flinuxnew.write(line.split('\n')[0] + ',\n')
line=flinux.readline()
line = line.split('\n')[0]
文件1数据内容
文件2
我的解答思路和尝试过的方法
如果文件2里面有2个ip或者3个ip的话,匹配完后写不进去
我想要达到的结果
如果有2个或多个ip,ip是以冒号分隔的 (;) 先拿第一个去列表匹配,匹配不到,拿第二个去匹配,匹配到了写入新文件,匹配不到就跳过