0

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.

1 Answer 1

3

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.

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

5 Comments

That does work thank you , but for some reason , even though it replaces the ones i want , it replaces it twice . so it always is , variable name STRING;STRING
The regex will replace each block of non-ascii. If you have ascii characters in the middle of a block, it will treat it as two separate blocks
I dont know i looked evertywhere and i believe the problem to be somewhere in my code but i cant seem to find it . for line in f: line = re.sub(r"[^\x1F-\x7F]+", 'STRING', line) print(line) if 'str' in line: break
Also the txt i use is extremely simple so i dont miss anything but it always gets replaced twice var TextBoxHandle STRING;STRING var TextBoxHandle STRING;STRING var TextBoxHandle STRING;STRING var TextBoxHandle STRING;STRING var TextBoxHandle STRING;STRING besides the last one
nevermind im so stupid i just realised what i was doing wrong

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.