Posts: 279
Threads: 107
Joined: Aug 2019
I'm getting invalid syntax with Python showing a carrot under the first colon. What did I do wrong?
print({}, ",", {}, ",", {}, ",", {}, ",", {}, ",", {}, ",", {:.2f}, ",", {:.2f}, ",", {:.2f}, ",", {}, ",",
{:.2f}, ",", {}, ",", {}, ",", {}, ",", {}, ",", {}, ",", {:.2f}, ",", {:.2f}, ",", {}, ",", {:.2f},
",", {:.2f}, ",", {:.2f}, ",", {:.2f}.format(spread_count, date, trade_date, L_spx, L_strike, L_price,
L_iv, L_delta, L_theta, S_price, S_iv, L_exp_mo, L_dte_trade_inception, S_exp_mo, S_dte_trade_inception,
spread_width, S_delta, S_theta, P_price, P_delta, P_theta, P_t_d, P_skew), file=strike_file) #testing f-string formatting
Posts: 6,823
Threads: 20
Joined: Feb 2020
That is not f-string formatting, that is the format() command and it is really, really wrong.
f-string looks like this:
count = 1
price = 1.2
name = "My name"
print(f"{count}, {name}, {price:.2f}")
Posts: 279
Threads: 107
Joined: Aug 2019
(Jan-13-2022, 11:10 PM)deanhystad Wrote: That is not f-string formatting, that is the format() command and it is really, really wrong.
f-string looks like this:
count = 1
price = 1.2
name = "My name"
print(f"{count}, {name}, {price:.2f}")
My mistake... I had that confused.
In any case, what's wrong with the syntax in my usage of the format command here? Note: the output is to a .csv file with over 20 columns, which is why I have all the ",", and such.
Posts: 7,324
Threads: 123
Joined: Sep 2016
Jan-14-2022, 01:29 AM
(This post was last modified: Jan-14-2022, 01:29 AM by snippsat.)
(Jan-13-2022, 11:41 PM)Mark17 Wrote: In any case, what's wrong with the syntax in my usage of the format command here? Almost all of it is wrong,learn to test smaller part before writing so much.
To give some example how it can be done
import csv
with open('birthday.csv') as csv_file:
reader = csv.reader(csv_file, delimiter=',')
for row in reader:
print('{:<15} {:<15} {:<15}'.format(*row)) Output: name department birthday month
John Smith Accounting November
Erica Meyers IT March
Pandas and tabulate
from tabulate import tabulate
import pandas as pd
df = pd.read_csv('birthday.csv')
print(tabulate(df, headers='keys', tablefmt='psql', showindex=False)) Output: +--------------+--------------+------------------+
| name | department | birthday month |
|--------------+--------------+------------------|
| John Smith | Accounting | November |
| Erica Meyers | IT | March |
+--------------+--------------+------------------+
CSV file used:
Output: name,department,birthday month
John Smith,Accounting,November
Erica Meyers,IT,March
Posts: 379
Threads: 2
Joined: Jan 2021
Jan-14-2022, 02:15 AM
(This post was last modified: Jan-14-2022, 02:15 AM by BashBedlam.)
It looks like this is what you're looking for:
spread_count = 27.24583
date = 'Jan 13 2022'
trade_date = 'yesterday'
L_spx = 144
L_strike = 'None'
print ('{0:.2f},{1},{2},{3}'.format(spread_count, date, trade_date, L_spx, L_strike)) Output: 27.25,Jan 13 2022,yesterday,144
Posts: 279
Threads: 107
Joined: Aug 2019
(Jan-14-2022, 02:15 AM)BashBedlam Wrote: It looks like this is what you're looking for:
spread_count = 27.24583
date = 'Jan 13 2022'
trade_date = 'yesterday'
L_spx = 144
L_strike = 'None'
print ('{0:.2f},{1},{2},{3}'.format(spread_count, date, trade_date, L_spx, L_strike)) Output: 27.25,Jan 13 2022,yesterday,144
Nice... looks like it works without the numbers too and I don't need all those extra commas in quotes to get the values into separate cells. Thanks!
Posts: 7,324
Threads: 123
Joined: Sep 2016
Jan-14-2022, 03:37 PM
(This post was last modified: Jan-14-2022, 03:37 PM by snippsat.)
With f-string that preferable and modern way to use now,it would be like.
spread_count = 27.24583
date = 'Jan 13 2022'
trade_date = 'yesterday'
L_spx = 144
L_strike = 'None'
print (f'{spread_count:.2f},{date},{trade_date},{L_strike}') Output: 27.25,Jan 13 2022,yesterday,None
Python 3's f-Strings: An Improved String Formatting Syntax (Guide)
Posts: 279
Threads: 107
Joined: Aug 2019
(Jan-14-2022, 03:37 PM)snippsat Wrote: With f-string that preferable and modern way to use now,it would be like.
spread_count = 27.24583
date = 'Jan 13 2022'
trade_date = 'yesterday'
L_spx = 144
L_strike = 'None'
print (f'{spread_count:.2f},{date},{trade_date},{L_strike}') Output: 27.25,Jan 13 2022,yesterday,None
Python 3's f-Strings: An Improved String Formatting Syntax (Guide)
I'll try to start using f-strings and getting used to this. Thanks!
|