Formatting a line with spaces at the beginning - python

Consider below piece of code
line = "I am writing a question"
print('{0: >10}'.format(line))
This does not work as expected. I expected output to be
' I am writing a question'
I know I can achieve this by other means like printing the spaces first using one print statement and then print the sentence. But curious to know what I might be doing wrong.

Your line is longer than 10 characters; the width is a minimal value and applies to the whole column. If you wanted to add 10 spaces, always, prefix these before the format:
print(' {0}'.format(line))
If you always wanted to right-align the string in a column of 33 characters (10 spaces and 23 characters for your current line), then set the column width to that instead:
print('{0:>33}'.format(line))
Now, when your line value is longer or shorter, the amount of whitespace will be adjusted to make the output 33 characters wide again.
Demo:
>>> line = "I am writing a question"
>>> print(' {0}'.format(line))
I am writing a question
>>> print('{0:>33}'.format(line))
I am writing a question
>>> line = "question"
>>> print('{0:>33}'.format(line))
question

There is a built in method :
https://docs.python.org/2/library/stdtypes.html#str.rjust
v = 'hi'
print v.rjust(4, ' ');
prints
' hi'

Related

How to switch \n to a newline in list? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
with open("C:\\test\\data1.txt") as f:
data = f.readlines()
tail = data[-10:]
I have a file name data1.txt inside folder test, when my program reads from the file it reads the whole content from the file and prints only the last 10 lines from the file.
['this is line number 21\n', 'this is line number 22\n', 'this is line
number 23\n', 'this is line number 24\n', 'this is line number 25\n',
'this is line number 26\n', 'this is line number 27\n', 'this is line
number 28\n', 'this is line number 29\n', 'this is line number 30\n']
I want to print the last 10 lines from the file with line breaks but cant figure how I can put inside a line break inside a list data structure.
for example :
To print txt file (data1.txt) like that :
this is line number 21
this is line number 22
this is line number 23
this is line number 24
without the \n and the list deceleration ([' '])
There are many answers already, but no one explained the problem.
Problem
\n is the newline character!
Explanation
In order to be able show the newline within a string literal, the escape sequence \n is used, e.g.:
>>> 'a\nb'
'a\nb'
>>> print('a\nb')
a
b
print function prints the string, if a string argument is passed to it, but if some other object is passed to it, it first has to be converted to a string, so print(x) is the same as print(str(x)).
When a list of strings is converted to string, that is done by calling repr on each of its items:
>>> ['a', 'a\nb']
['a', 'a\nb']
>>> str(['a', 'a\nb'])
"['a', 'a\\nb']"
>>> print("['a', 'a\\nb']")
['a', 'a\nb']
Solution
Now, if you want to print the last 10 lines, it means you should print each string in the list, not the list object itself, e.g.:
for s in list_of_strings:
print(s)
Now, since the s already contains a newline and print adds a newline itself, you should remove one of the newlines to make the solution complete:
for s in list_of_strings:
print(s.strip('\n'))
or:
for s in list_of_strings:
print(s, end='')
or create one string by concatenating items of the list and print that:
print(''.join(list_of_strings))
Best is a join (works on any version):
print(''.join(tail))
Just print tail like this:
print(*tail, sep='')
The sep argument stops the automatic space that is normally used as a separator between printed items.
Maybe this will help:
f.readlines()[-10:]
this way you will get last 10 lines.
Or just:
data = f.readlines()[-10:]
for d in data:
print(d) # will print everything on new line

.strip('{}:'.format(key)) sometimes strips last characters [duplicate]

This question already has answers here:
Python strip unexpected behavior
(2 answers)
Closed 4 years ago.
value = div.xpath('normalize-space(.)').extract()[0].strip('{}:'.format(key)).strip()
The code above sometimes strips the last character from the word. After removing the code after extract() all the data came back fine but in a list.
Example :
Unknown from Duration: Unknown turns into unknow
Movie from Type: Movie turns into Movi
Why does this happen?
I tried this in Python shell and it also strips the last characters
>>> value = ['Type: Movie']
>>> value[0].strip('{}:'.format('Type')).strip()
'Movi'
I expect it to return Movie instead of e getting stripped.
It seems that this .strip('{}:'.format('Type')) is responsible. I removed the last strip() it only return data with spaces.
Edit: It seems that strip() takes characters in inputted string and remove them instead of removing exact strings. That is why the data came out broken. I think a string split then slice is good.
Edit 2:
Seems like answers by Austin and Pankaj Singhal is good and bug free for my use case.
Use a split on 'Type: ' and take the second item:
value = ['Type: Movie']
print(value[0].split('Type: ')[1])
# Movie
Talking about your code, strip is not meant for what you are trying to do. strip only removes characters from at the ends.
You could use lstrip (which returns a copy of the string with only leading characters removed), instead of strip (which returns a copy of the string with leading and trailing characters removed):
>>> 'Type: Movie'.lstrip("Type:").strip()
'Movie'
>>> 'Type: Something with Type'.lstrip("Type:").strip()
'Something with Type'
>>> 'Type: Something with Type:'.lstrip("Type:").strip()
'Something with Type:'
>>>
OR:
>>> value = ['Type: Movie']
>>> value[0][value[0].find(':')+2:]
'Movie'
>>>
And of course, this is another option similar to the first one, just using lstrip:
>>> value[0][value[0].find(':')+1:].lstrip()
'Movie'
>>>
OR:
>>> value[0].lstrip(value[0][:value[0].find(':')+2])
'Movie'
Note: here find can be replaced with index
str.strip does not strip that exact string, but each character in that string, i.e. strip("Type:") will remove each T, y, p, etc. from the beginning and end of the string.
Instead, you could use a regular expression with the ^ anchor to only match substrings at the beginning of the string.
>>> value = ['Type: Movie with Type: in its name']
>>> key = "Type"
>>> re.sub(r"^{}: ".format(key), "", value[0])
'Movie with Type: in its name'

Remove unmixed numbers from file

Say I have a file called input.txt that looks like this
I listened to 4 u2 albums today
meet me at 5
squad 4ever
I want to filter out the numbers that are on their own, so "4" and "5" should go but "u2" and "4ever" should remain the same. i.e the output should be
I listened to u2 albums today
meet me at
squad 4ever
I've been trying to use this code
for line in fileinput.input("input.txt", inplace=True):
new_s = ""
for word in line.split(' '):
if not all(char.isdigit() for char in word):
new_s += word
new_s += ' '
print(new_s, end='')
Which is pretty similar to the code I found here: Removing numbers mixed with letters from string
But instead of the wanted output I get
I listened to u2 albums today
meet me at 5
squad 4ever
As you can see there are two problems here, first only the first line loses the digit I want it to lose, "5" is still present in the second line. The second problem is the extra white space at the beginning of a new line.
I've been playing around with the code for a while and browsing stackoverflow, but can't find where the problem is coming from. Any insights?
str.split(' ') does not remove the trailing newlines from each line. They end up attached to the last word of the line. So for your first problem, the '5' doesn't get removed because it's actually '5\n', and the \n is not a digit.
The second problem is related. When you print the last word of each line, it contains that newline, plus you're adding a space on to the end. That space shows up as the first character of the next line.
The simplest solution is simply to change line.split(' ') to line.split(). Without any arguments, split() will remove all whitespace, including the newlines. You'll also need to remove the end='' from your print so that the newlines are added back in.
Just use regexp.
re.sub(r"\b\d+\b", "", input)
match any digit between word boundaries
Or to avoid double spaces:
re.sub(r"\s\d+\s", " ", input)
You can use regex:
data = open('file.txt').read()
import re
data = re.sub('(?<=\s)\d+(?=$)|(?<=^)\d+(?<=\s)|(\s\d+\s)', '', data)
Output:
I listened tou2 albums today
meet me at
squad 4ever

How to remove special characters except space from a file in python? [duplicate]

This question already has answers here:
Keeping only certain characters in a string using Python?
(3 answers)
Closed 7 months ago.
I have a huge corpus of text (line by line) and I want to remove special characters but sustain the space and structure of the string.
hello? there A-Z-R_T(,**), world, welcome to python.
this **should? the next line#followed- by# an#other %million^ %%like $this.
should be
hello there A Z R T world welcome to python
this should be the next line followed by another million like this
You can use this pattern, too, with regex:
import re
a = '''hello? there A-Z-R_T(,**), world, welcome to python.
this **should? the next line#followed- by# an#other %million^ %%like $this.'''
for k in a.split("\n"):
print(re.sub(r"[^a-zA-Z0-9]+", ' ', k))
# Or:
# final = " ".join(re.findall(r"[a-zA-Z0-9]+", k))
# print(final)
Output:
hello there A Z R T world welcome to python
this should the next line followed by an other million like this
Edit:
Otherwise, you can store the final lines into a list:
final = [re.sub(r"[^a-zA-Z0-9]+", ' ', k) for k in a.split("\n")]
print(final)
Output:
['hello there A Z R T world welcome to python ', 'this should the next line followed by an other million like this ']
I think nfn neil answer is great...but i would just add a simple regex to remove all no words character,however it will consider underscore as part of the word
print re.sub(r'\W+', ' ', string)
>>> hello there A Z R_T world welcome to python
you can try this
import re
sentance = '''hello? there A-Z-R_T(,**), world, welcome to python. this **should? the next line#followed- by# an#other %million^ %%like $this.'''
res = re.sub('[!,*)##%(&$_?.^]', '', sentance)
print(res)
re.sub('["]') -> here you can add which symbol you want to remove
A more elegant solution would be
print(re.sub(r"\W+|_", " ", string))
>>> hello there A Z R T world welcome to python this should the next line followed by another million like this
Here,
re is regex module in python
re.sub will substitute pattern with space i.e., " "
r'' will treat input string as raw (with \n)
\W for all non-words i.e. all special characters *&^%$ etc excluding underscore _
+ will match zero to unlimited matches, similar to * (one to more)
| is logical OR
_ stands for underscore
Create a dictionary mapping special characters to None
d = {c:None for c in special_characters}
Make a translation table using the dictionary. Read the entire text into a variable and use str.translate on the entire text.

How to print spaces in Python?

In C++, \n is used, but what do I use in Python?
I don't want to have to use:
print (" ").
This doesn't seem very elegant.
Any help will be greatly appreciated!
Here's a short answer
x=' '
This will print one white space
print(x)
This will print 10 white spaces
print(10*x)
Print 10 whites spaces between Hello and World
print(f"Hello{x*10}World")
If you need to separate certain elements with spaces you could do something like
print "hello", "there"
Notice the comma between "hello" and "there".
If you want to print a new line (i.e. \n) you could just use print without any arguments.
A lone print will output a newline.
print
In 3.x print is a function, therefore:
print()
print("hello" + ' '*50 + "world")
Any of the following will work:
print 'Hello\nWorld'
print 'Hello'
print 'World'
Additionally, if you want to print a blank line (not make a new line), print or print() will work.
First and foremost, for newlines, the simplest thing to do is have separate print statements, like this:
print("Hello")
print("World.")
#the parentheses allow it to work in Python 2, or 3.
To have a line break, and still only one print statement, simply use the "\n" within, as follows:
print("Hello\nWorld.")
Below, I explain spaces, instead of line breaks...
I see allot of people here using the + notation, which personally, I find ugly.
Example of what I find ugly:
x=' ';
print("Hello"+10*x+"world");
The example above is currently, as I type this the top up-voted answer. The programmer is obviously coming into Python from PHP as the ";" syntax at the end of every line, well simple isn't needed. The only reason it doesn't through an error in Python is because semicolons CAN be used in Python, really should only be used when you are trying to place two lines on one, for aesthetic reasons. You shouldn't place these at the end of every line in Python, as it only increases file-size.
Personally, I prefer to use %s notation. In Python 2.7, which I prefer, you don't need the parentheses, "(" and ")". However, you should include them anyways, so your script won't through errors, in Python 3.x, and will run in either.
Let's say you wanted your space to be 8 spaces,
So what I would do would be the following in Python > 3.x
print("Hello", "World.", sep=' '*8, end="\n")
# you don't need to specify end, if you don't want to, but I wanted you to know it was also an option
#if you wanted to have an 8 space prefix, and did not wish to use tabs for some reason, you could do the following.
print("%sHello World." % (' '*8))
The above method will work in Python 2.x as well, but you cannot add the "sep" and "end" arguments, those have to be done manually in Python < 3.
Therefore, to have an 8 space prefix, with a 4 space separator, the syntax which would work in Python 2, or 3 would be:
print("%sHello%sWorld." % (' '*8, ' '*4))
I hope this helps.
P.S. You also could do the following.
>>> prefix=' '*8
>>> sep=' '*2
>>> print("%sHello%sWorld." % (prefix, sep))
Hello World.
rjust() and ljust()
test_string = "HelloWorld"
test_string.rjust(20)
' HelloWorld'
test_string.ljust(20)
'HelloWorld '
Space char is hexadecimal 0x20, decimal 32 and octal \040.
>>> SPACE = 0x20
>>> a = chr(SPACE)
>>> type(a)
<class 'str'>
>>> print(f"'{a}'")
' '
Tryprint
Example:
print "Hello World!"
print
print "Hi!"
Hope this works!:)
this is how to print whitespaces in python.
import string
string.whitespace
'\t\n\x0b\x0c\r '
i.e .
print "hello world"
print "Hello%sworld"%' '
print "hello", "world"
print "Hello "+"world
Sometimes, pprint() in pprint module works wonder, especially for dict variables.
simply assign a variable to () or " ", then when needed type
print(x, x, x, Hello World, x)
or something like that.
Hope this is a little less complicated:)
To print any amount of lines between printed text use:
print("Hello" + '\n' *insert number of whitespace lines+ "World!")
'\n' can be used to make whitespace, multiplied, it will make multiple whitespace lines.
In Python2 there's this.
def Space(j):
i = 0
while i<=j:
print " ",
i+=1
And to use it, the syntax would be:
Space(4);print("Hello world")
I haven't converted it to Python3 yet.
A lot of users gave you answers, but you haven't marked any as an answer.
You add an empty line with print().
You can force a new line inside your string with '\n' like in print('This is one line\nAnd this is another'), therefore you can print 10 empty lines with print('\n'*10)
You can add 50 spaces inside a sting by replicating a one-space string 50 times, you can do that with multiplication 'Before' + ' '*50 + 'after 50 spaces!'
You can pad strings to the left or right, with spaces or a specific character, for that you can use .ljust() or .rjust() for example, you can have 'Hi' and 'Carmen' on new lines, padded with spaces to the left and justified to the right with 'Hi'.rjust(10) + '\n' + 'Carmen'.rjust(10)
I believe these should answer your question.

Categories

Resources