如何将python代码的输出重定向到文件

时间:2018-12-31 08:47:31

标签: python

如何将python代码的输出写入同一python脚本中的文件

# 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='')

我当前的输出是:

  

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”,

     

磁盘,“ EMSA_C”,“ C:”,“系统”,“ F”,“ F”,“ 0”,“ 0”,“ 75”,“ 95”,

     

DISK,“ EMSA_D”,“ D:”,“数据”,“ 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”,

我的要求是将此输出输出到没有任何空行的文件中。请帮助我是python新手。

我的输入excel在这里:

input excel

1 个答案:

答案 0 :(得分:1)

您可以在全球范围内使用(适用于Python 3.0或更高版本)

from sys import stdout
stdout = open("logfilepath", "w")

print("output")

对于单个打印语句,您可以使用(对于Python 2.5或更高版本)

log = open("logfilepath", "w")

print >> log, "output"

写完输出后,只需确保调用close()。另外,也可以按照@Davis Herring的注释提示,将输出直接写到文件中,

log = open("logfilepath", "w")

log.write("output")

写完输出后,还需要调用close()