简体   繁体   English

Python 用其他字符串替换所有非ASCII字符串

[英]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我一直在寻找解决我的问题的任何类型的解决方案,唯一接近的是如果我已经知道非 ASCII 字符串,如何替换特定的行,但我想要做的是一个循环中的每一行txt 文件以查找每个非 ASCII 字符串并将其替换为单词 STRING

variable Name 'ƒ variable2 Name2 ƒŠ‡变量名称 'ƒ 变量 2 名称 2 ƒŠ‡

I have only found people removing all ascii and filling the blanks in between.我只发现人们删除了所有 ascii 并填补了两者之间的空白。

Try the Python standard library re.sub :试试 Python 标准库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.然后对文件中的每一行使用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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