Python代码以格式化方式附加文件

时间:2017-04-04 14:58:38

标签: python file formatting python-2.6

我想使用python在格式化的输出中写入/追加到文件中,我怎么能用特定的颜色/粗体/斜体来编写文件中的文本..等等

当前输出:

text0   = sample
text1   = <property><name>hbase.regionserver.wal.codec</name><value>org.apache.hadoop</value>
recovery_directory = "warning":2147483648   
text3   = recovery_dir
text4   = manager_log_dir

预期产出:

text0              = sample
text1              = <property>
                     <name>hbase.regionserver.wal.codec</name>
                     <value>org.apache.hadoop</value>
recovery_directory = "warning":2147483648  
text3              = recovery_dir
text4              = manager_log_dir

代码:

def fappend(f,text):
        check=os.path.isfile(f)
        text=str(text) + "\n"
        if check == False:
           f1=open(f,'w')
        else:
           f1=open(f,'a')
        f1.write(text)
        f1.close()
        return

def format(item,length):
  out = item.ljust(length)
  return out;


def main():

rname = api    
for c in rname.get_config(view='full'):
    name = c.name
    val = c.value
    if val != None:
       text = format(config.name,5)
       item = text + "=  " + val
       fappend(file, item)


if __name__ == "__main__":
   main()

0 个答案:

没有答案