string-formatting

Quote string value in F-string in Python

Quote string value in F-string in Python Question: I’m trying to quote one of the values I send to an f-string in Python: f’This is the value I want quoted: ‘{value}” This works, but I wonder if there’s a formatting option that does this for me, similar to how %q works in Go. Basically, I’m …

Total answers: 2

String format empty string caused extra space in print

String format empty string caused extra space in print Question: I want to string format a sentence as below: integer = 1 if integer != 1: n_val, book = integer, ‘books’ else: n_val, book =”, ‘book’ print(f’Fetch the top {n_val} children {book}.’) and I expected to see: Fetch the top 3 children books. or Fetch …

Total answers: 1

Have Python output .csv with space-padded, fixed width, but comma-separated columns?

Have Python output .csv with space-padded, fixed width, but comma-separated columns? Question: Let’s say I have CSV data as note below; let’s call this original.csv: name,value1,value2 firstname,34326408129478932874,553 secondname_a_very_long_one,65,123987 thirdname_medium,9686509933423,33 Basically, it’s either single word text (no space separation, so no need for quoting) or numbers (here integers, but could be floats with decimals or scientific …

Total answers: 1

Pandas magic with ugly CSV format

Pandas magic with ugly CSV format Question: An ancient atomic simulation software is producing really ugly CSV file, which I want to import to pandas dataframe. The format looks like this: ITEM: TIMESTEP 0 ITEM: NUMBER OF ATOMS 491 ITEM: BOX BOUNDS pp pp pp 0.0000000000000000e+00 2.8000000000000000e+01 0.0000000000000000e+00 2.8000000000000000e+01 0.0000000000000000e+00 2.8000000000000000e+01 ITEM: ATOMS id type …

Total answers: 1

How to run a command with %?

How to run a command with %? Question: I am trying to run command git log origin/master..HEAD –format=format:"%H" in python as below but running into below error,I tried to escape % but that doesn’t fix the error, any idea how to fix it? def runCmd2(cmd): logger.info("Running command %s"%cmd) proc = Popen(cmd ,universal_newlines = True, shell=True, …

Total answers: 3

Create table in Sqlite3 dynamically

How to create table dynamically from user input? Question: I am creating a wishlist app using Tkinter and sqlite3. I want the user to be able to create tables in database by imputing names. For that I connected a button to this function: def create_table(table_name): connection = sql.connect(f'{directory}main.sqlite’) cursor = connection.cursor() cursor.execute("CREATE TABLE ? (name …

Total answers: 1

Pyplot – format label in math mode of a variable

Pyplot – format label in math mode of a variable Question: g = 0.0 for i in range(0,5,1): plt.plot(a,b,label=’$K_{0:.2f}(x)$’.format(g)) g += 0.31 I do not manage to get the whole number in subscript in math mode, it displays just the first in subscript. How can this issue be solved? Asked By: cerv21 || Source Answers: …

Total answers: 1

Iterating over list of raw strings isn't displaying correctly in a plot title

Iterating over list of raw strings isn't displaying correctly in a plot title Question: I have a series of labels for a range of subplots, each of which is a raw string: labels = [r"$beta$", r"$alpha$", r"$omega$"] I want my plots to have the Greek letters displayed, but when I do: fig, axs = plt.subplots(1, …

Total answers: 1

Store formatted strings, pass in values later?

Store formatted strings, pass in values later? Question: I have a dictionary with a lot of strings. Is it possible to store a formatted string with placeholders and pass in a actual values later? I’m thinking of something like this: d = { "message": f"Hi There, {0}" } print(d["message"].format("Dave")) The above code obviously doesn’t work …

Total answers: 3

Why are double curly braces used instead of backslash in python f-strings?

Why are double curly braces used instead of backslash in python f-strings? Question: We usually use the backslash to escape illegal characters. For example, escaping the double quotes. >>> """ == ‘"’ True In f-strings, curly braces are used for placeholding. To represent a curly brace, the braces are doubled. For example, >>> f"{{}}" == …

Total answers: 2