写多行在Python中不起作用

时间:2019-01-25 08:17:41

标签: python

我正在尝试在文本文件中写多行。

代码如下:

directory = 'C:\SPAN'
with open(os.path.join(directory, 'SPANscript.txt'), 'w') as OPATH:
    OPATH.writelines([r'Load C:\SPAN\RiskFiles\%s\SGX.%s.s.pa2'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")),
                      r'Load C:\SPAN\RiskFiles\%s\cfe.%s.s.pa2'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")), 
                      r'Load C:\SPAN\RiskFiles\%s\cme.%s.s.pa2'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")), 
                      r'Load C:\SPAN\RiskFiles\%s\hkex.%s.s.pa2'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")),
                      r'Load C:\SPAN\RiskFiles\%s\Jsc%s_1700.pa2'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")), 
                      r'Load C:\SPAN\Positions\%s\CME_Span_pos_%s.pos'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y-%m-%d")),
                      r'Load C:\SPAN\Positions\%s\HKFE_Span_pos_%s.pos'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y-%m-%d")),
                      r'Load C:\SPAN\Positions\%s\OSE_Span_pos_%s.pos'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y-%m-%d")),
                      r'Load C:\SPAN\Positions\%s\SGX_Span_pos_%s.pos'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y-%m-%d")),
                      r'Load C:\SPAN\Positions\%s\XCME_Span_pos_%s.pos'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y-%m-%d")),
                      r'Load C:\SPAN\Positions\%s\XNYM_Span_pos_%s.pos'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y-%m-%d")),
                      'Calc',
                      r'SaveCalcSummary C:\SPAN\Reports\%s\%s.csv' %(rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")),
                      r'Save C:\SPAN\Reports\%s\%s.xml' %(rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")),
                      r'LogSave C:\SPAN\Reports\%s\logtest.txt'%rundate.strftime("%Y-%m-%d")])

数据像这样一行,不确定原因。

enter image description here

关注此帖子:write multiple lines in a file in python 需要一些指导。

3 个答案:

答案 0 :(得分:4)

您可以使用'\n'连接字符串列表:

OPATH.write(
    '\n'.join(
        [r'Load C:\SPAN\RiskFiles\%s\SGX.%s.s.pa2' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y%m%d")),
         r'Load C:\SPAN\RiskFiles\%s\cfe.%s.s.pa2' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y%m%d")),
         r'Load C:\SPAN\RiskFiles\%s\cme.%s.s.pa2' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y%m%d")),
         r'Load C:\SPAN\RiskFiles\%s\hkex.%s.s.pa2' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y%m%d")),
         r'Load C:\SPAN\RiskFiles\%s\Jsc%s_1700.pa2' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y%m%d")),
         r'Load C:\SPAN\Positions\%s\CME_Span_pos_%s.pos' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y-%m-%d")),
         r'Load C:\SPAN\Positions\%s\HKFE_Span_pos_%s.pos' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y-%m-%d")),
         r'Load C:\SPAN\Positions\%s\OSE_Span_pos_%s.pos' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y-%m-%d")),
         r'Load C:\SPAN\Positions\%s\SGX_Span_pos_%s.pos' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y-%m-%d")),
         r'Load C:\SPAN\Positions\%s\XCME_Span_pos_%s.pos' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y-%m-%d")),
         r'Load C:\SPAN\Positions\%s\XNYM_Span_pos_%s.pos' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y-%m-%d")),
         'Calc',
         r'SaveCalcSummary C:\SPAN\Reports\%s\%s.csv' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y%m%d")),
         r'Save C:\SPAN\Reports\%s\%s.xml' % (rundate.strftime("%Y-%m-%d"), rundate.strftime("%Y%m%d")),
         r'LogSave C:\SPAN\Reports\%s\logtest.txt' % rundate.strftime("%Y-%m-%d")]
    )
)
OPATH.write('\n') # write the trailing newline

答案 1 :(得分:2)

您需要在字符串中添加换行符。

例如:

directory = 'C:\SPAN'
with open(os.path.join(directory, 'SPANscript.txt'), 'w') as OPATH:
    OPATH.writelines([r'Load C:\SPAN\RiskFiles\%s\SGX.%s.s.pa2'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")) + "\n",
                      r'Load C:\SPAN\RiskFiles\%s\cfe.%s.s.pa2'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")) + "\n", 
                      r'Load C:\SPAN\RiskFiles\%s\cme.%s.s.pa2'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")) + "\n", 
                      r'Load C:\SPAN\RiskFiles\%s\hkex.%s.s.pa2'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")) + "\n",
                      r'Load C:\SPAN\RiskFiles\%s\Jsc%s_1700.pa2'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")) + "\n", 
                      r'Load C:\SPAN\Positions\%s\CME_Span_pos_%s.pos'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y-%m-%d")) + "\n",
                      r'Load C:\SPAN\Positions\%s\HKFE_Span_pos_%s.pos'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y-%m-%d")) + "\n",
                      r'Load C:\SPAN\Positions\%s\OSE_Span_pos_%s.pos'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y-%m-%d")) + "\n",
                      r'Load C:\SPAN\Positions\%s\SGX_Span_pos_%s.pos'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y-%m-%d")) + "\n",
                      r'Load C:\SPAN\Positions\%s\XCME_Span_pos_%s.pos'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y-%m-%d")) + "\n",
                      r'Load C:\SPAN\Positions\%s\XNYM_Span_pos_%s.pos'% (rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y-%m-%d")) + "\n",
                      'Calc' + "\n",
                      r'SaveCalcSummary C:\SPAN\Reports\%s\%s.csv' %(rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")) + "\n",
                      r'Save C:\SPAN\Reports\%s\%s.xml' %(rundate.strftime("%Y-%m-%d"),rundate.strftime("%Y%m%d")) + "\n",
                      r'LogSave C:\SPAN\Reports\%s\logtest.txt'%rundate.strftime("%Y-%m-%d")]) + "\n"

答案 2 :(得分:1)

错误是您没有 tell python包含新行,所以它没有这样做。

directory = 'C:\SPAN'
with open(os.path.join(directory, 'SPANscript.txt'), 'w') as OPATH:
    OPATH.writelines([r'some text here.', '\n', # this creates a new line
                      r'more text here.', '\n\n, # two new lines  
                      r'another line of text.', '\t'
                      # you can also create tab breaks/indents
                      ])