简体   繁体   中英

When I query MySQL I get a result with \u3000 in it

I'm connecting to a MySQL database, using utf8mbs, with the following code:

def name():

    with conn.cursor() as cursor:

        sql = "select name from fake_user where id = 147951"
        cursor.execute(sql)
        interentname = cursor.fetchall()
        for i in interentname:
            i = str(i)
            new_name = i.strip("',)")
            new_name = cc.strip("('")

    # return new_name.encode('utf8').decode('unicode_escape')
    return re.sub("[\u3000]", "", new_name)


print(name())

This keeps printing ♚\ \  恏😊 , I want to know how to get rid of the \  part in that.

The above code doesn't get rid of the \  though, why is that?

  • interentname is a tuple
  • new_name is a str string

How do I decode this properly?

You are turning each row, a tuple, into a string representation:

for i in interentname:
    i = str(i)

Don't do that. A tuple is a sequence of values, and for your specific query, there will be only a single value in it, the value for the name column. Index into the tuple to get the single value:

for row in interentname:
    name = row[0]

You can also use tuple assignment:

for row in interentname:
    name, = row

Note the comma after name there, it tells Python that row must be a sequence with one value and that that one value should be assigned to name . You can even do this in the for loop target:

for name, in interentname:
    print(name)

interentname is a sequence of tuples, not just a single tuple, so each iteration, you get a value like:

>>> row = ('♚\u3000\u3000 恏😊',)

The \  codepoints in there are U+3000 IDEOGRAPHIC SPACE characters, which Python will always echo as \\uxxxx escapes when the string is represented (as anything will be inside the standard containers).

By turning a tuple into a string, you then capture the representation as a string:

>>> str(row)
>>> str(row)
"('♚\\u3000\\u3000 恏😊',)"

Python represents tuples using valid Python syntax, and uses valid Python syntax for strings too. But removing the tuple syntax from that output (so the "(' at the start and ',) at the end) does not give you the proper string value back.

Indexing the tuple object gives you the value in it:

>>> row[0]
'♚\u3000\u3000 恏😊'
>>> print(row[0])
♚   恏😊

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM