简体   繁体   中英

Python How to print the text read from a file without the escape char?

The python code is like this. I try to read the data from a file. But when I print it I get the output with square bracket and escape char . If I just print the content which I copy from the data file. It show normally. The result is like this: [u'\\n\\n\\xefhello']

How to fix this problem. This is the result window. I try to remove the square bracket but it doesn't work. And I try to encode the string by utf-8 , neither working.

And the type str I read from the data file is <type 'unicode'> .

20161212185023.bmp

from bs4 import BeautifulSoup
import re

f = open('sgsres.txt', 'r')
content = f.read()
cleantext = BeautifulSoup(content, "lxml").text
cleantext = re.sub('[\[\]]', '', cleantext)
print cleantext

--- update --- @Boris

I rewrite the code according to your answer but the the output I get is like this. Every char output on a single line and the escape char seems still print out as raw text. 20161212193059.bmp

--- update ---

This question is fixed when I encode the string with acsii and ignore the error like this. text = text.encode('ascii',errors='ignore') But I still don't know why.

Square brackets indicate that you are working with a list .

As for the string s that are inside that list , this is what you can do:

from bs4 import BeautifulSoup
import re

f = open('sgsres.txt', 'r')
content = f.read()
cleantext = BeautifulSoup(content, "lxml").text
for item in cleantext:
    item = item.strip()
    print item

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