Misaligment while formatting with escape t "\t" [duplicate] - python

This question already has answers here:
Incorrect column alignment when printing table in Python using tab characters
(4 answers)
Closed 1 year ago.
I'm learning Python through "Python Projects for Beginners" by Connor Milliken. In the first project "Creating a Recipt Printing Program" there is this section
# creating a product and price for three itens
p1_name, p1_price = "Books", 49.95
p2_name, p2_price = "Computer", 579.99
p3_name, p3_price = "Monitor", 124.89
# create a print statement for each product
print("\t{}\t\t${}".format(p1_name.title(), p1_price))
print("\t{}\t\t${}".format(p2_name.title(), p2_price))
print("\t{}\t\t${}".format(p3_name.title(), p3_price))
The lines are equal but for the second line the price is misaligned as if it has another \t. The problem was the same in jupyter notebook and Atom + terminal. If you just delete one '\t' the problem is solved but you can't really understand what happened.

Don't think of a tab as inserting a specific number of spaces in the string (it doesn't). Instead, you are giving control over whoever displays the string, since they are the ones that decide where the tab stops are.
If you want precise control, use fixed-width padded format specifiers instead. For example,
print(" {:>10} {:>6}".format(p1_name.title(), p1_price))
This assumes that 10 characters is wide enough for any title and 6 characters is wide enough for any price.

Related

Atom will not autocomplete closed quotes when typing f-strings

I'm using the Atom editor as I make my way through Learn Python the Hard Way.
For some reason, Atom will autogenerate closed quotes in a basic string.
x = "There are 10 types of people."
But it will not when the string starts with f.
x = f"There are {types_of_people} types of people.
When I get to the end of the f-string, I have to add the closed quotes manually, which actually generates a new set of both open and closed quotes.
x = f"There are {types_of_people} types of people.""
I then have to delete one set of quotes to make the f-string work, which is not a big deal but annoying.
I've tried putting a space between the f and the open quotes. Doing so generates the closed quotes but results in a coding error.
I haven't had this problem with the Sublime or Mu editors. Any advice on how to fix it?
The following link has a suggested solution:
F string autocomplete python
However, the solution is almost two years old. I'm wondering if there's a more recent fix that is less convoluted.

Python a string with tabs and integers is different outside print [duplicate]

This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed 1 year ago.
I'm merging some tab delimited files and the printed output is incorrect but if I access the string in a REPL it looks fine. Here's how it looks:
fh=open('out.vcf')
for line in fh:
i+=1
if i == 29401:
print(line)
AAEX03025909.1 1068 . T C 0 42 5
Then looking at it without print:
line
'AAEX03025909.1\t1405\t.\tC\tT\t\t\t\t\t\t0\t0\t0\t0\t0\t0\t0\t0\t10\t9\n'
When I look at out.vcf in less, it looks like the output of print. Why am I getting different outputs? I want the string that is produced without print. Using a comma instead of a tab solves the problem, but I'd like to keep it as tab delimited
there's always going to be some difference between how data is represented and how it's stored; practically, the values are stored as binary, but represented depending on the encoding .. in this case, you're seeing \t (ASCII character 9) represented both ways
print() will show the file with its encoding (which you can change), while simply echoing the file will show you the Python repr() interpretation
>>> "\t"
'\t'
>>> ord("\t")
9
>>> print("\t")
>>> repr("\t")
"'\\t'"
>>> print(repr("\t"))
'\t'

Delete last character printed in python 3.6 [duplicate]

This question already has answers here:
Delete last printed character python
(3 answers)
Closed 4 years ago.
I'm trying to delete the last character I've printed from the previous line in python, e.g if I do print ("hello world")
I will get hello world outputted, but I'm wondering after it's been outputted would there be a way to remove the d at the end of the output so it would now only say hello worl... in short is there a way to remove the end character of an already printed line?
Thanks
There is no way to perform operations on data that has been output since it is not stored in memory as a variable. You can however overwrite the location on the screen (if you are dealing with something like a terminal) that it is printed on to 'remove' the old data.
Here is a function you can add and call in your code to send text to a specific location on the terminal display:
import sys
def PrintLocate(row, column, text):
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (row, column, text))
sys.stdout.flush()
# Elsewhere
PrintLocate(13, 37, "Hello World")
PrintLocate(13, 37 + len("Hello Worl"), " ")

how to format a line in python? [duplicate]

This question already has answers here:
Format output string, right alignment
(8 answers)
Closed 8 years ago.
I have a tuple contains both string and float values (which are read from a txt file and calculated by me, respectively) and I want to write it to another txt file.
variables = (line.split()[0],line.split()[1], velocity) #velocity is a floating number, others are #string
output_file.write('%s %s %4.2f \n' % variables)
These lines are in a for loop. I want to align each variable in each line as right justified. How can I do that?
Please note that string items don't have same character in each line.
Python has several ways to format strings. In the form you use, you get right alignment by specifying a field length and optional padding. Python right aligns to fit the field length by default. Your float calculation already has a field length, so just decide on a length for the strings also. Its easy if you already have a max field size in mind. Here is an example of 10 spaces per string:
'%10s %10s %4.2f \n' % variables

Python- ArcMap - Calculate Fields

I am python newbie and I am trying to count the number of words in a column (Name) in ArcMap by using
!NAME!.count(' ') +
1
but I run into problems with strings like :
First N' Infant Care Center "Baby World"
type.exceptions.Syntaxerror,
even if I use " ",same problem I encounter when I am using other methods like split, strip etc.
Try
len(!Name!.split(" "))
If that doesn't work...let us know which feature it fails on and maybe more sample data?
Try encoding the string, arc does funny things with their string encoding...
!NAME!.encode('ascii', 'ignore').count(' ') + 1
Python can not easily handle mixed double and single quotes, so it's best if you first remove them.
One way to do this is to add another field (say newName, calculate it to have the same values as "Name" field, by doing just !NAME!. I am assuming you don't want to alter the Name field.
Then within editing mode, use find and replace to replace all quotes " or ' in that new column with nothing (just don't type anything in the replace and run replace all).
Now if you use that same approach you used with this new column/field, the problem won't occur.
!newName!.count(' ') + 1

Categories

Resources