Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7175597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:19:55+00:00 2026-05-28T16:19:55+00:00

I’m writing a simple regular expression parser for the output of the sensors utility

  • 0

I’m writing a simple regular expression parser for the output of the sensors utility on Ubuntu. Here’s an example of a line of text I’m parsing:

temp1:        +31.0°C  (crit = +107.0°C)

And here’s the regex I’m using to match that (in Python):

temp_re = re.compile(r'(temp1:)\s+(\+|-)(\d+\.\d+)\W\WC\s+' 
                     r'\(crit\s+=\s+(\+|-)(\d+\.\d+)\W\WC\).*')

This code works as expected and matches the example text I’ve given above. The only bits I’m really interested in are the numbers, so this bit:

(\+|-)(\d+\.\d+)\W\WC

which starts by matching the + or - sign and ends by matching the °C.

My question is, why does it take two \W (non-alphanumeric) characters to match ° rather than one? Will the code break on systems where Unicode is represented differently to mine? If so, how can I make it portable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:19:57+00:00Added an answer on May 28, 2026 at 4:19 pm

    Possible portable solution:

    Convert input data to unicode, and use re.UNICODE flag in regular expressions.

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import re
    
    
    data = u'temp1:        +31.0°C  (crit = +107.0°C)'
    temp_re = re.compile(ur'(temp1:)\s+(\+|-)(\d+\.\d+)°C\s+' 
                         ur'\(crit\s+=\s+(\+|-)(\d+\.\d+)°C\).*', flags=re.UNICODE)
    
    print temp_re.findall(data)
    

    Output

    [(u'temp1:', u'+', u'31.0', u'+', u'107.0')]
    

    EDIT

    @netvope allready pointed this out in comments for question.

    Update

    Notes from J.F. Sebastian comments about input encoding:

    check_output() returns binary data that sometimes can be text (that should have a known character encoding in this case and you can convert it to Unicode). Anyway ord(u’°’) == 176 so it can not be encoded using ASCII encoding.

    So, to decode input data to unicode, basically* you should use encoding from system locale using locale.getpreferredencoding() e.g.:

    data = subprocess.check_output(...).decode(locale.getpreferredencoding())
    

    With data encoded correctly:

    you’ll get the same output without re.UNICODE in this case.


    Why basically? Because on Russian Win7 with cp1251 as preferredencoding if we have for example script.py which decodes it’s output to utf-8:

    #!/usr/bin/env python
    # -*- coding: utf8 -*-
    
    print u'temp1: +31.0°C  (crit = +107.0°C)'.encode('utf-8')
    

    And wee need to parse it’s output:

    subprocess.check_output(['python', 
                             'script.py']).decode(locale.getpreferredencoding())
    

    will produce wrong results: 'В°' instead °.

    So you need to know encoding of input data, in some cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.