简体   繁体   中英

How do I stop Python from escaping the backslashes when reading text from a file?

I'm having trouble getting text from a file to be in the same format as the text in a string. Example:

>>> a = 'Hello\tWorld'
>>> list(a)
['H', 'e', 'l', 'l', 'o', '\t', 'W', 'o', 'r', 'l', 'd']

That's fine. Now when I read the same characters from a file, I get a different result...

with open('file.txt', 'r') as f:
    a = f.read()

>>> list(a)
['H', 'e', 'l', 'l', 'o', '\\', 't', 'W', 'o', 'r', 'l', 'd']

The tab is gone. Now I have an escaped backshlash and at, instead of a tab. And the number of elements in the list is different.

How do I read a file, and keep the tab?

BTW, I'm working on character counting and would like to be counting tabs as one thing, not two.

As far as I can tell you're reading in the string as a raw string. In order to convert a raw string to a normal string it can be decoded with the 'string_escape' codec:

with open('file.txt','r') as f: 
     a  = f.read().decode('string_escape')

I wasn't able to recreate the same txt file but this works when testing with a raw string:

a = r'hello\tworld'
list(a.decode('string_escape')) #outputs ['h', 'e', 'l', 'l', 'o', '\t', 'w', 'o', 'r', 'l', 'd']

hope this helps!

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