generate functionSun, 13 Nov 2022

create a python script that opens a csv file named book1. Take column 1 from book1 to create a table named as OBJECT_IP. Print this table to screen. Then prompt the user for input to provide a text value to be used as the description named OBJECT_DESC. Print this to the screen. Then take the OBJECT_IP value replace any . with an _ then remove any numbers and the . from the beginning of the value up to the second _ and print this to the screen. Then add each row of the column to the end of the description with an _ in between the description and row value and retain this new value as OBJECT_NAME to match the row of the table that the value of OBJECT_IP is used. Then create a file called objects with the output of OBJECT_NAME's on one line than with the next line has the word host + the OBJECT_IP on this line. Then take all the OBJECT_NAME values and nest them in a group with the group name being the value of OBJECT_DESC.

import csv import re # Importing the CSV file with open('book1.csv', newline='') as csvfile: data = csv.reader(csvfile, delimiter=',', quotechar='|') # Setting the data to the first column for row in data: for e in row: # Initializing the list OBJECT_IP = [e] # Printing the first column print(e) # Prompting the user for input OBJECT_DESC = input('Please enter a description: ') # Printing the description print(OBJECT_DESC) # Using RegEx to remove the . and numbers for ip in OBJECT_IP: e = ip # Removing the . and numbers x = re.sub('[.0123456789]', '', e) # Printing the new IP list print(x) # Writing the data to a file objects = open('objects', 'w')

Questions about programming?Chat with your personal AI assistant