简体   繁体   中英

Automatically remove leading u'...' in Python strings

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?

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

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