使用 Python 代码出现在我的 CSV 中的补充引号

supplementary quotes appearing in my csv using python code

提问人:sunny 提问时间:8/22/2022 最后编辑:MattDMosunny 更新时间:8/23/2022 访问量:25

问:

我做了一个代码来生成多个地址并将其导出为 csv

import csv
import ipaddress
import random
from random import shuffle

LAN = ipaddress.ip_network('192.168.0.0/16')
WAN1 = ipaddress.ip_network('11.10.8.0/22')
WAN2 = ipaddress.ip_network('12.10.8.0/22')

LAN_IP_Adresses = [ IP_LAN for IP_LAN in LAN.hosts()]
WAN1_IP_Adresses = [ IP_WAN1 for IP_WAN1 in WAN1.hosts()]
WAN2_IP_Adresses = [ IP_WAN2 for IP_WAN2 in WAN2.hosts()]

index_IP_GW = len(WAN1_IP_Adresses)-1

locations_list=['Abidjan','Abu Dhabi','Adana','Adelaide', 'Ahmadabad','Algiers','Amsterdam','Ankara','Anshan','Athens','BANGKOK','BUCHAREST','BUDAPEST','Bagdad','Bandung','Bangalore','Bangkok','Barcelona','Beirut','Belgrade','Bern','Bogota','Brasilia','Brazzaville','Brussels','Bursa','CAIRO','CARACAS','CONAKRY','Canberra','Casablanca','Changchun','Chengdu','Chicago','Copenhagen','Dakar','MINSK','Madrid','Medina','Nairobi','Napoli','Montreal',
'Odessa','Omdurman','Osaka','Ottawa','PYONGYANG','Paris','Pekin', 'Perth','Philadelphia','Phoenix','Pimpri Chinchwad','Porto','Porto Alegre','QUITO','Qingdao','Rabat','Rajkot','Riadh','Rio de Janeiro','Rome','SANTIAGO','Salvador','Samara','San Antonio','San Francisco','Sao Paulo','Sendai','Seongnam','Seoul','Shanghai','Singapore','Sydney','Taiyuan','Tehran','Tijuana','Tokyo','Toronto','Moscou','Moscow','Mumbai (Bombay)','Munich','México','Milan',
 'Tripoli','Tunis','Vienna','Warsaw','Wuhan','Xian','Yaounde','Yokohama', 'Zapopan','hong kong','Dallas','Delhi','Doha','Dublin','Durban','Ecatepec','Frankfurt','Fukuoka','Giza','Hamburg','Havana','Helsinki','Houston','Hyderabad','Istanbul','Jaipur','Jakarta','Jeddah','Johannesburg','KIEV','Kaduna','Kano','Kazan','Kuala Lumpur''Kyoto','LUANDA','Lahore','Lanzhou','Le Caire','Leon','Lima','Lisbon','London','Los Angeles','Lyon','MANILA','Melbourne','New York']
#Site_Nmb=1
def initial_Sites_list_generator(filename='SITES_CI.csv', Number_of_Sites=1000):
    file_to_output = open(filename,'w',newline='')
    csv_writer = csv.writer(file_to_output,delimiter=',')
    
    Site_Nbr=1
    index = 0 
 
    csv_writer.writerow(["SITE_NAME", "SERIAL_NUMBER",'"LAN_IP_ADDRESS"','"WAN_IP_ADDRESS1"','"WAN_IP_ADDRESS2"','"GATEWAY_IP_ADDRESS1"','"GATEWAY_IP_ADDRESS2"','"ROUTE_REFLECTOR"','"LOCATIONS"','"HARDWAREMODEL"','"LANINT"','"WANINT1"','"WANINT2"','"BW_OUT"','"BW_IN"'])
    
    for i in range(1,Number_of_Sites+1):
        shuffle(locations_list)
        location = random.choice(locations_list)
        csv_writer.writerow(['"SITE'+ str(Site_Nbr)+'"',"2e70129bde9c4426b9213d4408c300",f'"{(LAN_IP_Adresses[index])}"',f'"{str(WAN1_IP_Adresses[index])}"',f'"{str(WAN2_IP_Adresses[index])}"',f'"{str(WAN1_IP_Adresses[index_IP_GW])}"',f'"{str(WAN2_IP_Adresses[index_IP_GW])}"','"False"',f'"{location}"','"ONEv600"','"gigabitethernet0/2"','"gigabitethernet0/0"','"gigabitethernet0/1"','"10"','"20"'])
        Site_Nbr = Site_Nbr+1
        index = index+1
    file_to_output.close()
initial_Sites_list_generator('SITES_OVP.csv', 1000)

但是我在CSV中添加了不必要的引号

enter image description here

python csv 双引号

评论

2赞 MattDMo 8/22/2022
您自己在调用中添加引号。删除它们。csv_writer
0赞 sunny 8/22/2022
如果您打算删除分隔符,我已经尝试过,但得到了相同的结果

答:

0赞 MattDMo 8/22/2022 #1

您正在自己添加额外的引号。在循环中,更改此行:for

csv_writer.writerow(['"SITE'+ str(Site_Nbr)+'"',"2e70129bde9c4426b9213d4408c300",f'"{(LAN_IP_Adresses[index])}"',f'"{str(WAN1_IP_Adresses[index])}"',f'"{str(WAN2_IP_Adresses[index])}"',f'"{str(WAN1_IP_Adresses[index_IP_GW])}"',f'"{str(WAN2_IP_Adresses[index_IP_GW])}"','"False"',f'"{location}"','"ONEv600"','"gigabitethernet0/2"','"gigabitethernet0/0"','"gigabitethernet0/1"','"10"','"20"'])

对此:

csv_writer.writerow(['SITE'+ str(Site_Nbr)+"2e70129bde9c4426b9213d4408c300",
                    f'{(LAN_IP_Adresses[index])}',
                    f'{str(WAN1_IP_Adresses[index])}',
                    f'{str(WAN2_IP_Adresses[index])}',
                    f'{str(WAN1_IP_Adresses[index_IP_GW])}',
                    f'{str(WAN2_IP_Adresses[index_IP_GW])}',
                    'False',
                    f'{location}',
                    'ONEv600',
                    'gigabitethernet0/2',
                    'gigabitethernet0/0',
                    'gigabitethernet0/1',
                    '10',
                    '20'])

CSV 编写器已根据需要向字符串添加引号。

评论

0赞 sunny 8/22/2022
谢谢你的回答,但像这样,我认为不会有引号或单引号,但我在其他工具中使用这个 csv,它只接受双引号
0赞 MattDMo 8/22/2022
@sunny 初始化 CSV 编写器对象时,请使用 .doublequote=True
0赞 sunny 8/22/2022 #2

我做了

csv_writer = csv.writer(file_to_output,delimiter=",",quoting=csv.QUOTE_ALL)

它奏效了!