python列表推导式的应用
#!/usr/bin/python3 import pandas as pd def main(): df_iport = pd.read_excel('./ip-port.xlsx', sheet_name=0) for i in range(df_iport['目标地址'].__len__()): str_ip = df_iport['目标地址'][i].replace('\n','') ip_list = str_ip.split(',') str_port = str(df_iport['TCP目的端口号'][i]).replace('\n','') port_list = str_port.split(',') ip_port = [ip.strip()+':'+port.strip() for ip in ip_list if len(ip) != 0 for port in port_list if len(port) != 0 and port != 'nan'] for item in ip_port: print(item) if __name__ == '__main__': main()
strip()去除字符串开头或者结尾的空格
split()将字符串拆分为列表