Home > Software engineering >  On string with white space, python command .isspace() gives result false
On string with white space, python command .isspace() gives result false

Time:12-29

I am trying to split the string with white space got from xpath but it didn't work

 x="78646182309549(5)" 
 x.split() 

so I checked if there is white space on the string using

x.isspace()

it returns False

Html:

<td ><a href="https://www.zto.com/?num=78646182309549(5)" target="_blank" style="font-weight:bold;font-size:14px;">78646182309549(5)</a></td>


 x = response.xpath("(//td[@class='tl']//a)[1]/text()").extract_first()
 print(x)
 print(x.isspace())

Desired output:

x = ['78646182309549','(5)']
y = x[0]
print(y)
78646182309549

CodePudding user response:

isspace() returns True if the all string is a space

'   '.isspace() # True

and to split by space you need to use

split(' ')

But this won't help here since is a single character in your text. You separate this text using re.split

x = "78646182309549(5)"
x = re.split(r'(()', x)
x1, x2 = x[0], ''.join(x[1:])
# or
x = [x[0], ''.join(x[1:])]

print(x[0]) # 78646182309549
print(x[1]) # (5)

Edit

As suggested by @mozway you can do

x = "78646182309549(5)"
x = re.split(r'(?=()', x)
print(x) # ['78646182309549', '(5)']

CodePudding user response:

The isspace() method returns True if all the characters in a string are whitespaces, otherwise False.

Technically there are no spaces in x:

[y for y in x]

['7', '8', '6', '4', '6', '1', '8', '2', '3', '0', '9', '5', '4', '9', '(', '5', ')']

This :

"78646182309549(5)".split()

returns :

['78646182309549(5)']

You can split with this:

x="78646182309549(5)".split('(')
#output ['78646182309549', '5)']
x[0]='78646182309549'
  • Related