Reputation: 37
How to write the output of python code to a file inside the same python script
# Reading an excel file using Python
import xlrd
# Give the location of the file
loc = ("C:\\Users\\212515181\\Desktop\\Dual_Mode_cfgctrl.xlsx")
# To open Workbook
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
# Getting Root record value
print("ROOT,"+ '"CONFIGMDL"')
# Getting Comput record entries
for k in range(2,sheet.nrows):
if sheet.cell_value(k,2)=="COMPUT" and sheet.cell_value(k,0)!="#":
print ('\n')
print("COMPUT,",end='')
for i in range (4,14,2):
print('"%s"' % sheet.cell_value(k,i),",",end='',sep='')
# Getting Direct record entries
for k in range(2,sheet.nrows):
if sheet.cell_value(k,2)=="DIRECT" and sheet.cell_value(k,0)!="#":
print ('\n')
print("DIRECT,",end='')
for i in range (4,14,2):
print('"%s"' % sheet.cell_value(k,i),",",end='',sep='')
# Getting Disk record entries
for k in range(2,sheet.nrows):
if sheet.cell_value(k,2)=="DISK" and sheet.cell_value(k,0)!="#":
print ('\n')
print("DISK,",end='')
for i in range (4,22,2):
print('"%s"' % sheet.cell_value(k,i),",",end='',sep='')
# Getting Domain record entries
for k in range(2,sheet.nrows):
if sheet.cell_value(k,2)=="DOMAIN" and sheet.cell_value(k,0)!="#":
print ('\n')
print("DOMAIN,",end='')
for i in range (4,12,2):
print('"%s"' % sheet.cell_value(k,i),",",end='',sep='')
# Getting Equipments record entries
for k in range(2,sheet.nrows):
if sheet.cell_value(k,2)=="EQUIP" and sheet.cell_value(k,0)!="#":
print ('\n')
print("EQUIP,",end='')
for i in range (4,20,2):
print('"%s"' % sheet.cell_value(k,i),",",end='',sep='')
# Getting Local system record entries
for k in range(2,sheet.nrows):
if sheet.cell_value(k,2)=="LOCSYS" and sheet.cell_value(k,0)!="#":
print ('\n')
print("LOCSYS,",end='')
for i in range (4,10,2):
print('"%s"' % sheet.cell_value(k,i),",",end='',sep='')
# Getting Appset record entries
for k in range(2,sheet.nrows):
if sheet.cell_value(k,2)=="APPSET" and sheet.cell_value(k,0)!="#":
print ('\n')
print("APPSET,",end='')
for i in range (4,18,2):
print('"%s"' % sheet.cell_value(k,i),",",end='',sep='')
# Getting Dbsset record entries
for k in range(2,sheet.nrows):
if sheet.cell_value(k,2)=="DBSSET" and sheet.cell_value(k,0)!="#":
print ('\n')
print("DBSSET,",end='')
for i in range (4,14,2):
print('"%s"' % sheet.cell_value(k,i),",",end='',sep='')
# Getting Process record entries
for k in range(2,sheet.nrows):
if sheet.cell_value(k,2)=="PRCESS" and sheet.cell_value(k,0)!="#":
print ('\n')
print("PRCESS,",end='')
for i in range (4,12,2):
print('"%s"' % sheet.cell_value(k,i),",",end='',sep='')
# Getting Taskset record entries
for k in range(2,sheet.nrows):
if sheet.cell_value(k,2)=="TSKSET" and sheet.cell_value(k,0)!="#":
print ('\n')
print("TSKSET,",end='')
for i in range (4,10,2):
print('"%s"' % sheet.cell_value(k,i),",",end='',sep='')
My Current output is :
ROOT,"CONFIGMDL"
COMPUT,"USADS1","T","F","F","60",
COMPUT,"USADS2","T","F","F","60",
DIRECT,"EMSA","1","30","10","60",
DIRECT,"EMSB","2","30","10","60",
DISK,"EMSA_C","C:","System","F","F","0","0","75","95",
DISK,"EMSA_D","D:","Data","F","F","0","0","75","95",
DOMAIN,"HOST","60","T","F",
EQUIP,"USADS1","3.0","","","T","F","","",
LOCSYS,"EMSB","2","T",
APPSET,"EMSHOST","EMSHOST","EMSA SERVER","1","T","F","T",
DBSSET,"EMSHOST","0","60","1","1",
PRCESS,"MRS","T","F","1",
PRCESS,"CFGMONI","T","F","1",
PRCESS,"CFGPING","T","F","1",
PRCESS,"PROCMAN","F","T","",
TSKSET,"EMSHOST","1","1",
My Requirement is to this output to a file without any empty lines. Please help me guys I am new to python.
My input excel is here:
Upvotes: 3
Views: 766
Reputation: 10320
Globally you can use (with Python 3.0 or later)
from sys import stdout
stdout = open("logfilepath", "w")
print("output")
For individual print statements you can use (with Python 2.5 or later)
log = open("logfilepath", "w")
print >> log, "output"
Just make sure to call close()
when you are done writing output. Alternatively you can write the output directly to the file, as the comment from @Davis Herring hinted,
log = open("logfilepath", "w")
log.write("output")
which will also require a call to close()
when you are done writing output.
Upvotes: 1