0

I am reading a text file with the following sentence:

"So whether you’re talking about a Walmart or an IKEA or a Zara, you are really interested in keeping the cost low, keeping the process very efficient."

my code:

files = "*.txt" for pathname in glob.glob(files): with open(pathname,'r') as singlefile: data = "".join(singlefile.readlines()) data = re.sub(r"(?<=\w)\n", " ", data) data = re.sub(r",\n", ", ", data) print data 

result I got is

"So whether you鈥檙e talking about a Walmart or an IKEA or a Zara, you are really interested in keeping the cost low, keeping the process very efficient. That gives us operational excellence."

Can anyone tell me what is wrong? Thanks!

6
  • Have a look at the encoding. It looks like the ' is not recognized. Commented May 30, 2014 at 2:19
  • 3
    You need to read the file using the encoding it was saved as. Commented May 30, 2014 at 2:19
  • how do I know its encoding? Commented May 30, 2014 at 2:23
  • 3
    Either you find out from the person who gave you the file, or you guess. Commented May 30, 2014 at 2:27
  • 1
    P.S. It would help if you print repr(data) so we can see the exact bytes. Commented May 30, 2014 at 2:29

1 Answer 1

0

If you get the encoding right (for this also look here, where they also describe an encoding guess list - which is a neat idea), it works just fine. I have tried it with:

import re with open("words.txt",'r') as singlefile: data = "".join(singlefile.readlines()) data = re.sub(r"(?<=\w)\n", " ", data) data = re.sub(r",\n", ", ", data) print data 

And in the file "words.txt" is this:

 So whether you’re talking about a Walmart or an IKEA or a Zara, you are really interested in keeping the cost low, keeping the process very efficient. 

This is the output:

>>> runfile('E:/programmierung/python/spielwiese/test.py', wdir=r'E:/programmierung/python/spielwiese') So whether you’re talking about a Walmart or an IKEA or a Zara, you are really interested in keeping the cost low, keeping the process very efficient. >>> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.