Python Forum
How did one column get left-justified?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How did one column get left-justified?
#1
So I was going about tweaking the formatting of this .csv file (in Excel) generated by Python and then at one point, the S_exp_mo column (that is, column 4 with zero-indexing) became left-justified. I have two print lines: one for the first day of each trade and another for every other day in the trade. Only the first day shows this left-justification and only for S_exp_mo.

Here is the print line for the first day of each trade:

                        print(f'{trade_status}, {spread_count}, {date}, 0, {S_exp_mo}, {S_dte_orig}, {spread_width}, {spx_orig:.2f}, {L_strike}, \
                            {(L_strike-spx_orig):.2f}, 0, {L_iv_orig*100:.2f}, {H_skew_orig*100:.2f}, {P_price_orig:.2f}, {P_price_orig:.2f}, 0, \
                            {P_theta*100:.2f}, {P_delta_orig*100:.2f}, {P_t_d_orig:.2f}, 0', file=strike_file) #testing f-string formatting
Here is the print line for all subsequent days of each trade:

                    print(f'{trade_status}, {spread_count}, {datetime.utcfromtimestamp(current_date*86400).strftime("%Y-%m-%d")}, {L_dte_orig-L_dte}, \
                        {S_exp_mo}, {S_dte}, {spread_width}, {spx:.2f}, {L_strike}, {(L_strike-spx):.2f}, {(spx-spx_orig):.2f}, {L_iv*100:.2f}, \
                        {H_skew*100:.2f}, {P_price_orig:.2f}, {P_price:.2f}, {(P_price-P_price_orig)*100:.2f}, {P_theta*100:.2f}, {P_delta*100:.2f}, \
                        {P_t_d:.2f}, {ROI_current*100:.2f}', file=strike_file) #testing f-string formatting
Can you tell why the S_dte variable is left-justified in the first case and right-justified in the second?
Reply
#2
It's also possible that left-justified is default and the second case (for all other lines) became right-justified. In looking closer at the Excel file, it seems a bit strange that when I bring up the cell and look at the formula bar, there are 25 spaces before the text appears. Why would that be?
Reply
#3
Right justify >
Left justify <
Center justify ^
greeting = "Hello"
print(f"{greeting:<10}\n{greeting:>10}\n{greeting:^10}\n{greeting:10}")
Output:
Hello Hello Hello Hello
Reply
#4
I just fixed it. I changed the second print line to this:

                    print(f'{trade_status}, {spread_count}, {datetime.utcfromtimestamp(current_date*86400).strftime("%Y-%m-%d")}, {L_dte_orig-L_dte}, {S_exp_mo}, \
                        {S_dte}, {spread_width}, {spx:.2f}, {L_strike}, {(L_strike-spx):.2f}, {(spx-spx_orig):.2f}, {L_iv*100:.2f}, \
                        {H_skew*100:.2f}, {P_price_orig:.2f}, {P_price:.2f}, {(P_price-P_price_orig)*100:.2f}, {P_theta*100:.2f}, {P_delta*100:.2f}, \
                        {P_t_d:.2f}, {ROI_current*100:.2f}', file=strike_file) #testing f-string formatting
If you look closely, all I did was move {S_exp_mo} from after first backslash to before first backslash.

Here's my novice theory for what is going on...

This is an indented block, which is not reflected on the website here. In my code, first word--"print"--is indented five tabs, which is 20 spaces. {S_dte} is now indented 24 spaces--plus one additional space between the comma and backslash in the previous line.

For whatever reason, numbers seem to be insensitive to the whitespace. S_exp_mo is a string, though (3-letter abbreviation for month as generated by .strftime('%b') ). Could strings be SENSITIVE to that whitespace? That would explain the leading 25-or-so spaces I see to the left of the letters in the .csv file.

This would also mean had I just left it as one really long line without backslashes (I just did that for readability), I would have never run into this issue.
Reply
#5
With the f' string the "indentation" are actually spaces in the string.
print(f"{1} {2} \
    {3} {4} \
....{5} {6}")
Output:
1 2 3 4 ....5 6
I think you should do something like this.
line = (
    f'{trade_status}, {spread_count} '
    f'{datetime.utcfromtimestamp(current_date*86400).strftime("%Y-%m-%d")} '
    f'{L_dte_orig-L_dte}, {S_exp_mo},  {S_dte}, {spread_width}, {spx:.2f} '
    f'{L_strike}, {(L_strike-spx):.2f}, {(spx-spx_orig):.2f}, {L_iv*100:.2f} '
    f'{H_skew*100:.2f}, {P_price_orig:.2f}, {P_price:.2f}, {(P_price-P_price_orig)*100:.2f} '
    f'{P_theta*100:.2f}, {P_delta*100:.2f},  {P_t_d:.2f}, {ROI_current*100:.2f}'}
print(line, file=strike_file) 
Reply
#6
(Feb-26-2022, 08:42 PM)deanhystad Wrote: With the f' string the "indentation" are actually spaces in the string.
print(f"{1} {2} \
    {3} {4} \
....{5} {6}")
Output:
1 2 3 4 ....5 6
I think you should do something like this.
line = (
    f'{trade_status}, {spread_count} '
    f'{datetime.utcfromtimestamp(current_date*86400).strftime("%Y-%m-%d")} '
    f'{L_dte_orig-L_dte}, {S_exp_mo},  {S_dte}, {spread_width}, {spx:.2f} '
    f'{L_strike}, {(L_strike-spx):.2f}, {(spx-spx_orig):.2f}, {L_iv*100:.2f} '
    f'{H_skew*100:.2f}, {P_price_orig:.2f}, {P_price:.2f}, {(P_price-P_price_orig)*100:.2f} '
    f'{P_theta*100:.2f}, {P_delta*100:.2f},  {P_t_d:.2f}, {ROI_current*100:.2f}'}
print(line, file=strike_file) 

I see posts about backslashes and f-strings. I'll read up.

And I'll try out your suggestion.

Thanks!!
Reply
#7
A backslash at the end of a line tells Python that the next line is a continuation of the current line.. This would work fine if you didn't mind writing your print statement like this.
print(f'{trade_status}, {spread_count}, {datetime.utcfromtimestamp(current_date*86400).strftime("%Y-%m-%d")}, {L_dte_orig-L_dte}, {S_exp_mo}, \
{S_dte}, {spread_width}, {spx:.2f}, {L_strike}, {(L_strike-spx):.2f}, {(spx-spx_orig):.2f}, {L_iv*100:.2f}, \
{H_skew*100:.2f}, {P_price_orig:.2f}, {P_price:.2f}, {(P_price-P_price_orig)*100:.2f}, {P_theta*100:.2f}, {P_delta*100:.2f}, \
{P_t_d:.2f}, {ROI_current*100:.2f}', file=strike_file)
This is just fine and doesn't break any Python indentation rule because as far as Python is concerned, this is just one really long line.

If you want to indent the lines you can take advantage of Python's automatic string concatenation and do what I did.

This might also be a place where old fashioned format statements might be a better choice.
Mark17 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyQT5 - align left frohr 7 3,999 May-07-2022, 09:56 PM
Last Post: deanhystad
  Explanation of the left side of this statement please rascalsailor 3 2,512 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  How to left align logging messages Mekala 3 6,880 Jun-28-2020, 04:04 PM
Last Post: bowlofred
  How to left align the columns SriRajesh 6 3,980 Dec-28-2019, 04:04 PM
Last Post: SriRajesh
  Why is left mouse click not working? yeto 3 6,207 Jul-15-2019, 05:23 AM
Last Post: Yoriz
  str.format rounding to the left of the decimal ClassicalSoul 2 2,495 Mar-27-2019, 11:12 AM
Last Post: perfringo
  Move a character left or right in a file. DreamingInsanity 4 4,872 Mar-21-2019, 07:52 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020