1

I am working on migrating an old Python code base to Python3.

There are many strings which have the "u" prefix. Example u'Umlaut üöö'

Is there an automated way to remove the leading "u"?

A simple regex is does not work:

u'schibu': u' at the end must not get removed.

Example2:

Multiline: '''foo

schibu'''

Is there maybe a way which works without a regex, but via parsing the python syntax?

Update

My code needs to be compatible with Python2 and Python3 for some months.

The files already contain from __future__ import unicode_literals

2
  • 2
    Why bother? The u prefix doesn't break anything. Commented Jan 31, 2022 at 10:21
  • 1
    There is no reason to remove the u prefix. It was added back to Python in version 3.3 for compatibility reasons. Leave it as is, then once the code has been definitely migrated from 2 to 3, you can remove it later on (a formatter like Black even does that for you, for example). Commented Jan 31, 2022 at 10:54

1 Answer 1

3

Using 2to3 tool unicode fixer should do that.

unicode

Renames unicode to str.

Dry run with sample spam.py file

eggs = u'foo'

in shell:

$ 2to3 --fix unicode spam.py

output

root: Generating grammar tables from /usr/lib/python2.7/lib2to3/PatternGrammar.txt
RefactoringTool: Refactored spam.py
--- spam.py     (original)
+++ spam.py     (refactored)
@@ -1 +1 @@
-eggs = u'foo'
+eggs = 'foo'
RefactoringTool: Files that need to be modified:
RefactoringTool: spam.py

EDIT: Note, you can run just a single fixer as shown above (in a dry run) and it will apply only the respective change.

Sign up to request clarification or add additional context in comments.

2 Comments

My code needs to be compatible with Python2 and Python3 for some months. AFAIK this means I can't use 2to3.
You can run just a single fixer - unicode and it will change only this. But if you need to support both 2 and 3 for whatever reason, are you sure that removing u prefix wouldn't break the python2 code - i.e. I would expect it is there for reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.