-1

I am trying to write multiple lines to the text file.

The code looks like this:

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")])

The data comes in one line like this, not sure why.

enter image description here

Followed this post: write multiple lines in a file in python Need some guidance on this.

4
  • 1
    What error does it give you? Commented Jan 25, 2019 at 8:22
  • @DDS, showed u my output. Commented Jan 25, 2019 at 8:27
  • you need to feed it a new line escape '\n' code at the end of each string Commented Jan 25, 2019 at 8:30
  • I suggest you to use Notepad++, so you can also see 'special characters' as CR and LF as in notepad are not clearly visible. Another thing is to post stuff as text instead of as pictures Commented Jan 25, 2019 at 8:34

3 Answers 3

4

You can join the list of strings with '\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
Sign up to request clarification or add additional context in comments.

1 Comment

This is far more pythonic than adding a fixed '\n' to every line by hand.
2

You need to add newline char to the string.

Ex:

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"

Comments

1

The mistake is that you didn't tell python to include a new line, so it's not doing it.

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
                      ])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.