0

I am trying to write many lines to a text file but it contains many dates for each line.

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

I would like to make it neater. Saw this post: Python - multiple %s string, but where do i put the format portion? at each line or can i put it outside?

2
  • Which version of Python do you have? Commented Jan 25, 2019 at 3:37
  • I am using Spyder and version 2.7.9 Commented Jan 25, 2019 at 3:38

3 Answers 3

0

I would recommend using str.format instead of old-style formatting.

directory = r'C:\SPAN'

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

Of course, if you could manually modify your file names to have {0} instead of %s, you could ditch the .replace('%s', '{0}').

Sign up to request clarification or add additional context in comments.

2 Comments

Word of caution: file.writelines() won't add new lines at the end of each element in the sequence despite what its name might suggest. It's an example of a bad API but the rationale was to have it as a counterpart to file.readlines() which does the opposite and keeps the EOL character(s) so that the result can be used directly in file.writelines().
Also, it might be a good idea to store rundate.strftime('%Y-%m-%d') outside of the loop and just reuse it instead of executing it for each line.
0

You can use a list comprehension here. For example

...
lines = [
    r'Load C:\SPAN\RiskFiles\%s\SGX.%s.s.pa2', 
    r'Load C:\SPAN\RiskFiles\%s\cfe.%s.s.pa2',
    ...]
formatted_date = rundate.strftime("%Y-%m-%d")
OPATH.writelines([line % (formatted_date, formatted_date) for line in lines])

It is also a good idea to switch to the newer method of string formatting as @Tomothy32 suggested.

Comments

0

I don't see the value in adding complexity by introducing loops when you have a preset list of lines to write - all you need is a neater way to inject the date into your template, so something like this:

directory = r'C:\SPAN'
date = rundate.strftime('%Y-%m-%d')

with open(os.path.join(directory, 'SPANscript.txt'), 'w') as f:
    f.write(r'Load C:\SPAN\RiskFiles\{d}\SGX.{d}.s.pa2' '\n'
            r'Load C:\SPAN\RiskFiles\{d}\cfe.{d}.s.pa2' '\n'
            r'Load C:\SPAN\RiskFiles\{d}\cfe.{d}.s.pa2' '\n'
            r'Load C:\SPAN\RiskFiles\{d}\cme.{d}.s.pa2' '\n'
            r'Load C:\SPAN\RiskFiles\{d}\hkex.{d}.s.pa2' '\n'
            r'Load C:\SPAN\RiskFiles\{d}\Jsc{d}_1700.pa2' '\n'
            r'Load C:\SPAN\Positions\{d}\CME_Span_pos_{d}.pos' '\n'
            r'Load C:\SPAN\Positions\{d}\HKFE_Span_pos_{d}.pos' '\n'
            r'Load C:\SPAN\Positions\{d}\OSE_Span_pos_{d}.pos' '\n'
            r'Load C:\SPAN\Positions\{d}\SGX_Span_pos_{d}.pos' '\n'
            r'Load C:\SPAN\Positions\{d}\XCME_Span_pos_{d}.pos' '\n'
            r'Load C:\SPAN\Positions\{d}\XNYM_Span_pos_{d}.pos' '\n'
            'Calc\n'
            r'SaveCalcSummary C:\SPAN\Reports\{d}\{d}.csv' '\n'
            r'Save C:\SPAN\Reports\{d}\{d}.xml' '\n'
            r'LogSave C:\SPAN\Reports\{d}\logtest.txt'.format(d=date))

should be more than enough, plus it will handle newlines properly unlike file.writelines() (see my comment to @Tomothy32's answer).

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.