Home > Enterprise >  supplementary quotes appearing in my csv using python code
supplementary quotes appearing in my csv using python code

Time:08-24

I did a code to generate multiple addresses and export it in 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)

but i got unnecessary quotes added in my csv

enter image description here

CodePudding user response:

You are adding the extra quotes yourself. In your for loop, change this line:

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"'])

to this:

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'])

The CSV writer already adds quotes to strings as appropriate.

CodePudding user response:

I did

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

and it worked !

  • Related