简体   繁体   中英

Python replace all non-ASCII strings with other strings

I have been looking around for a while to any kind of solution to my problem the only thing close is how to replace a specific line if i know the non ASCII string already but what i would like to do is a loop for every line in a txt file to find every single non ASCII string and replace it with just the word STRING

variable Name 'ƒ variable2 Name2 ƒŠ‡

I have only found people removing all ascii and filling the blanks in between.

Try the Python standard library re.sub :

import re
txt = "‘ƒ variable2 Name2 ƒŠ‡"
txt = re.sub(r"[^\x1F-\x7F]+", "STRING", txt)
print(txt)

Then use this method for each line in your file.

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