How to prevent Python from adding\ before escape characters when reading files

Today suddenly encountered a problem, in the text processing need to remove some stop words, stop words are put in a txt file, one of the string is "\ xa0 ", read with python will automatically become "\\ xa0" . In the past, it was always preceded by an "r" string, but I really didn"t figure it out when reading this from a file.

Mar.24,2021

\ xa0 is a blank character, which is its hexadecimal representation. I guess you saved the string \ xa0 directly into the file. The right thing to do is to save the symbol itself.

>>> with open('new.py', 'w') as f:
...     f.write('\xa0')
...
1
>>> with open('new.py', 'r') as f:
...     content = f.read()
...
>>> content
'\xa0'

if you open it with a text editor, you can only see a blank character.

clipboard.png


agrees that python has automatically changed the file into streaming format when saving the file.


method 1, read the file in a binary way and parse the contents in detail to avoid this problem.

method 2, since I guess this may be a coding problem, it is recommended to test this problem with the errors parameter of the open function, which may be solved.

In addition, because there is no test, I am not sure whether you have actually added a backslash, or whether it is a display backslash caused by Python printing for display, such as the repr function.

Menu