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 8603137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:15:12+00:00 2026-06-12T02:15:12+00:00

I need to convert (in Python) a 4-byte char into some other character. This

  • 0

I need to convert (in Python) a 4-byte char into some other character. This is to insert it into my utf-8 mysql database without getting an error such as: “Incorrect string value: ‘\xF0\x9F\x94\x8E’ for column ‘line’ at row 1”

Warning raised by inserting 4-byte unicode to mysql shows to do it this way:

>>> import re
>>> highpoints = re.compile(u'[\U00010000-\U0010ffff]')
>>> example = u'Some example text with a sleepy face: \U0001f62a'
>>> highpoints.sub(u'', example)
u'Some example text with a sleepy face: '

However, I get the same error as the user in the comment, “…bad character range..” This is apparently because my Python is a UCS-2 (not UCS-4) build. But then I am not clear on what to do instead?

  • 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-06-12T02:15:13+00:00Added an answer on June 12, 2026 at 2:15 am

    In a UCS-2 build, python uses 2 code units internally for each unicode character over the \U0000ffff code point. Regular expressions need to work with those, so you’d need to use the following regular expression to match these:

    highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
    

    This regular expression matches any code point encoded with a UTF-16 surrogate pair (see UTF-16 Code points U+10000 to U+10FFFF.

    To make this compatible across Python UCS-2 and UCS-4 versions, you could use a try:/except to use one or the other:

    try:
        highpoints = re.compile(u'[\U00010000-\U0010ffff]')
    except re.error:
        # UCS-2 build
        highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
    

    Demonstration on a UCS-2 python build:

    >>> import re
    >>> highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
    >>> example = u'Some example text with a sleepy face: \U0001f62a'
    >>> highpoints.sub(u'', example)
    u'Some example text with a sleepy face: '
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert a python code into an equivalent java code. Python makes
I need to convert a zope 2 DateTime object into a Python datetime object.
I am trying to convert this to python. I just really need help with
I need fastest way to convert files from latin1 to utf-8 in python. The
I'm trying to convert some python code to java and need to setup a
I need to convert unicode strings in Python to other types such as unsigned
I need to convert a bunch of files to utf-8 in Python, and I
I need to convert any html entity into its ASCII equivalent using Python. My
I need to convert string into ASCII code. I'm using python. I did as
I need to convert strings in Python to other types such as unsigned and

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.