Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string parsing with re.search()
#4
(Jun-03-2020, 01:35 PM)snippsat Wrote: There is no u'' in Python 3,so follow advice over.
# Python 3.8
>>> s = u'\xbd' 
>>> s
'½'

# Can remove <u> make no difference
>>> s = '\xbd' 
>>> s
'½'
# Python 2.7
>>> s = u'\xbd' 
>>> s
u'\xbd'
>>> s.encode()
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xbd' in position 0: ordinal not in range(128)

# Try the obvious one  first  
>>> s.encode('utf-8')
'\xc2\xbd'
>>> print(s.encode('utf-8'))
½

# Make a guess
>>> print(s.encode('latin-1'))
½
On of the biggest changes moving to Python 3 was to make Unicode better Wink

Thanks for your help.

But I don't get where this will fit in my code?

Specifically what I am looking at is this:

<span class="rp-horseTable__pos__length">
<span>2½</span>
<span>[5½]</span>
</span>

I want what's in the square brackets, within the second nested span.
If I grab the whole lot by referencing the span class, I then run into the problem above when using re.search() on the square bracket. It's caused (apparently) by the fraction in the first span.

Can I get at the second span directly?

thanks
Reply


Messages In This Thread
string parsing with re.search() - by delahug - Jun-03-2020, 08:24 AM
RE: string parsing with re.search() - by snippsat - Jun-03-2020, 01:35 PM
RE: string parsing with re.search() - by delahug - Jun-03-2020, 09:23 PM
RE: string parsing with re.search() - by snippsat - Jun-03-2020, 10:20 PM
RE: string parsing with re.search() - by delahug - Jun-04-2020, 09:39 AM
RE: string parsing with re.search() - by snippsat - Jun-04-2020, 10:34 AM
RE: string parsing with re.search() - by delahug - Jun-04-2020, 07:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Learning:bs4, re.search] - RegEx string cutoff jarmerfohn 5 3,761 Nov-23-2019, 09:32 AM
Last Post: buran
  Regex search for string DBS 3 4,624 Feb-06-2017, 11:39 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020