Reputation: 478
I am reading an excel file and the parsing some data which i need to dump to the text file.
I want to create a text file in a certain format and write that into a text file.
I dont know if we can do that as an input like df[df['Storage Site'].str.contains(input('Please Enter SiteName: '))]
import pandas as pd
from pandas import read_excel
my_sheet = "test"
file_name = 'Lab_Storage_Installation_Tracker2.xlsx'
df = read_excel(file_name, sheet_name = my_sheet)
tv_node1_name = df[df['Storage Site'].str.contains('Munich')]['Hostname'].values[1]
tv_node2_name = df[df['Storage Site'].str.contains('Munich')]['Hostname'].values[3]
tv_node1_ip = df[df['Storage Site'].str.contains('Munich')]['IP Address'].values[0]
tv_node2_ip = df[df['Storage Site'].str.contains('Munich')]['IP Address'].values[3]
print("################# NODE DETAILS ####################")
print(tv_node1_name)
print(tv_node2_name)
print(tv_node1_ip)
print(tv_node2_ip)
print("")
print("################# CLUSTER DETAILS ####################")
tv_cluster_name = df[df['Storage Site'].str.contains('Munich')]['Hostname'].values[0]
tv_cluster_location = df[df['Storage Site'].str.contains('Munich')]['Storage Site'].values[0]
print(tv_cluster_name)
print(tv_cluster_location)
print("")
print("############## CLUSTER MANAGEMENT ##################")
tv_cluster_mgmt_ip = df[df['Storage Site'].str.contains('Munich')]['LIF IP'].values[0]
tv_cluster_mgmt_mask = df[df['Storage Site'].str.contains('Munich')]['Subnet'].values[0]
print(tv_cluster_mgmt_ip)
print(tv_cluster_mgmt_mask)
################# NODE DETAILS ####################
ptlab308n1
ptlab308n2
192.168.1.1
192.168.1.2
################# CLUSTER DETAILS ####################
ptlab308
Munich
############## CLUSTER MANAGEMENT ##################
192.1678.1.18
255.255.255.240
################# NODE DETAILS ####################
tv_node1_name = ptlab308n1
tv_node2_name = ptlab308n2
tv_node1_ip = 192.168.1.1
tv_node2_ip = 192.168.1.2
################# CLUSTER DETAILS ####################
tv_cluster_name = ptlab308
tv_cluster_location = Munich
############## CLUSTER MANAGEMENT ##################
tv_cluster_mgmt_ip = 192.1678.1.18
tv_cluster_mgmt_mask = 255.255.255.240
How I can achieve the above, any help would be much appreciated.
Sample data is Here
Upvotes: 1
Views: 68
Reputation: 8816
I think you are almost close, you just need to add the variable name's into print statement for your desired output link below:
print("################# NODE DETAILS ####################")
print("tv_node1_name =", tv_node1_name)
print("tv_node2_name =", tv_node2_name)
print("tv_node1_ip =", tv_node1_ip)
print("tv_node2_ip =", tv_node2_ip)
print("")
print("################# CLUSTER DETAILS ####################")
print("tv_cluster_name =", tv_cluster_name)
print("tv_cluster_location =", tv_cluster_location)
print("")
print("############## CLUSTER MANAGEMENT ##################")
print("tv_cluster_mgmt_ip =", tv_cluster_mgmt_ip)
print("tv_cluster_mgmt_mask =", tv_cluster_mgmt_mask)
and finally you might do
$ <you_pandas_script.py> > Desired.txt
You can define a variable like location = input('Enter your location: ')
which you can use as an input so, you don't need to amend your code every time.
Secondly, as want to write into a file hence you can use like with open(input('Enter Config File Name to be Created: '), 'a') as f_Obj:
and write all your print data into it.
import pandas as pd
from pandas import read_excel
location = input('Enter your location: ')
excl_Sheet = "test"
file_Name = 'Lab_Storage_Installation_Tracker2.xlsx'
df = read_excel(file_Name, sheet_name = excl_Sheet)
tv_node1_name = df[df['Storage Site'].str.contains(location)]['Hostname'].values[1]
tv_node2_name = df[df['Storage Site'].str.contains(location)]['Hostname'].values[3]
tv_node1_ip = df[df['Storage Site'].str.contains(location)]['IP Address'].values[1]
tv_node2_ip = df[df['Storage Site'].str.contains(location)]['IP Address'].values[3]
#print("################# CLUSTER DETAILS ####################")
tv_cluster_name = df[df['Storage Site'].str.contains(location)]['Hostname'].values[0]
tv_cluster_location = df[df['Storage Site'].str.contains(location)]['Storage Site'].values[0]
#print("############## CLUSTER MANAGEMENT ##################")
tv_cluster_mgmt_ip = df[df['Storage Site'].str.contains(location)]['IP Address'].values[0]
tv_cluster_mgmt_mask = df[df['Storage Site'].str.contains(location)]['Subnet'].values[0]
########## Write all the Variable data into a text file #############
with open(input('Enter Config File Name to be Created: '), 'a') as f_Obj:
print(f"################# NODE DETAILS ####################", file=f_Obj)
print(f"tv_node1_name={tv_node1_name}",file=f_Obj)
print(f"tv_node2_name={tv_node2_name}",file=f_Obj)
print(f"tv_node1_ip={tv_node1_ip}",file=f_Obj)
print(f"tv_node2_ip={tv_node2_ip}",file=f_Obj)
print(f'""', file=f_Obj)
print(f"################# CLUSTER DETAILS ####################", file=f_Obj)
print(f"tv_cluster_name={tv_cluster_name}", file=f_Obj)
print(f"tv_cluster_location={tv_cluster_location}", file=f_Obj)
print(f"tv_timezone={time_Zone}", file=f_Obj)
print(f'""', file=f_Obj)
print(f"############## CLUSTER MANAGEMENT ##################", file=f_Obj)
print(f"tv_cluster_mgmt_ip={tv_cluster_mgmt_ip}", file=f_Obj)
print(f"tv_cluster_mgmt_mask={tv_cluster_mgmt_mask}", file=f_Obj)
print(f'""', file=f_Obj)
print("File Creation Completed")
$ ./test_script.py
Enter your location: Munich
Upvotes: 2