As you can see in screenshot there is huge whitespaces between 4.0 out of 5 stars and 12,467 ratings. How to remove that whitespace?
I tried strip(), split(), replace() but i am not getting expected output
CodePudding user response:
You could use the re
library and re.sub()
function for this if all of that is being printed out from the stars
variable. So you need to import re
first. Then you can set your stars variable like this after you get the initial value.
stars = re.sub(r'\s ',' ',stars)
The r'\s '
is a regular expression that will match one or more consecutive whitespaces, and then will be replaced by the 2nd argument of just a single space.
CodePudding user response:
If I understand your question correctly, you want to remove the lines between "4.0 out of 5 stars" and "12,467 ratings".
in this case you can split lines of text and check if you want to keep the line or not using a for loop:
text_lines = []
for line in text.splitlines():
if line: # check if line is not empty
text_lines.append(line)
print("\n".join(text_lines))
you can ignore the line if there is only space characters in the line by removing the spaces and then check if the line is not empty after we removed the space character, to do that we can use line.Replace(" ", "")
:
text_lines = []
for line in text.splitlines():
if line.Replace(" ", ""):
text_lines.append(line)
print("\n".join(text_lines))
if you want to remove the space character at the start of each line you can also do that by finding the index of the first non-space character in the line, then remove everything before that index from the line:
if line.startswith(" "): # checks if the line starts with space, for optimization
first_non_space_character = line.replace(" ", "")[0] # removes every space from the line and gets the first non-space character
space_index = line.index(first_non_space_character) # gets the index of that character in the line
line = line[space_index:] # removes everything before space_index
so the final loop is:
text_lines = []
for line in text.splitlines():
if line.replace(" ", ""):
if line.startswith(" "):
first_non_space_character = line.replace(" ", "")[0]
space_index = line.index(first_non_space_character)
line = line[space_index:]
text_lines.append(line)
print("\n".join(text_lines)) # joins every line in `text_lines` with a new-line/enter character between them
and if you want everything in one line you can simply instead of joining lines with a new-line/enter character between them, it joins the line with a space between them.
to do this simply replace "\n".join(text_lines)
with " ".join(text_lines)
.
i hope this helps and sorry if i wrote something grammatically wrong, English is not my first language.