用Python写一个IP地址csv转txt文本文件更新RosMikrotik路由器地址列表

初级怪兽,请见笑。

import requests
import pandas
import os
import csv
# 保存csv
def save_csv():
    r = requests.get(‘https://regionip.aliyun.com/publish/abtn-ip2location.csv’)
    with open(r’taobaoip.csv’, ‘wb’)as f:
        f.write(r.content)
    return (r.content)
# 转换csv并保存
def csv_txt():
    data = pandas.read_csv(‘taobaoip.csv’, encoding=’utf-8′, names=[‘A’, ‘B’, ‘C’, ‘D’, ‘E’])
    slist = data[‘A’]
    atxt = open(‘taobaoip.txt’,’w’)
    count = 1
    for kt_line in slist:
        atxt.writelines(‘add address=’+kt_line)
       atxt.writelines(‘ disabled=no list=”TaoBaoIP”‘)
       atxt.writelines(‘\n’)
       count += 1
    atxt.close()
    print (f’总计更新了{count}IP’)
    with open(‘taobaoip.txt’,’r+’) as ftxt:
        contxt = ftxt.read()
        ftxt.seek(0,0)
        ftxt.write(‘/ip firewall address-list\n’+contxt)
    return
if __name__ == “__main__”:
    save_csv()
    csv_txt()

发布者

湘笃秀

曲水流觞,维湘笃秀