0

My current code, displays the results in both console and an output text file with the following statement

     fw.write("Number of files processed within 512\u00B1 1 samples:  "+str(count))

My output on the text file is "Number of files processed within 512xB1 1 samples: 328"

But sometimes I do get the correct output"Number of files processed with 512± 1 samples: 3"

Console/interpreter output is fine with a print("") function where I always get ±. Being scouring and trying with encoding statements, any ideas?

Masud Syed
  • 103
  • 1
  • 6

4 Answers4

1

Unless you specifically require your source file to contain only ASCII, or your editor doesn't support rendering this specific character, just literally write the character in the source code:

fw.write("Number of files processed within 512±1 1 samples:  "+str(count))

Otherwise, explicitly open the file with the utf8 encoding:

with open('file.txt', 'w', encoding='utf8') as fw:
    fw.write("Number of files processed within 512\u00B1 1 samples:  "+str(count))
Andy
  • 116
  • 2
  • 7
1
  1. Use Unicode strings.
  2. Declare your source encoding.
  3. Use the characters directly in the file if you like instead of escape codes.
  4. For printing, just print the Unicode string.
  5. For files, use io.open, declare the encoding (can be different than source and console), and write Unicode strings.
  6. Save the source in the source encoding.

Then, if your console encoding supports the character (even if the console is a different encoding than the source file), it will display correctly. Files will contain the correctly encoded character.

Example (works in Python 2 and 3):

#coding:utf8
from __future__ import unicode_literals,print_function
import io
count = 57
with io.open('out.txt','w',encoding='utf8') as fw:
    fw.write("Number of files processed within 512±1 samples: {}".format(count))
    print("Number of files processed within 512±1 samples: {}".format(count))

Output:

C:\temp>chcp              # Console is a different encoding!
Active code page: 437

C:\temp>py -2 x.py        # Python 2 displays correctly
Number of files processed within 512±1 samples: 57

C:\temp>py -3 x.py        # Python 3 displays correctly
Number of files processed within 512±1 samples: 57

C:\temp>chcp 65001        # Change to output file encoding (UTF-8)
Active code page: 65001

C:\temp>type out.txt      # Content of file is correct.
Number of files processed within 512±1 samples: 57
Mark Tolonen
  • 132,868
  • 21
  • 152
  • 208
1

First, don't use Notepad, Microsoft refused to add UTF-8 support to it even on Windows 10.

Yes, enforcing use of UTF-8 is recommend when reading or writing files.

sorin
  • 137,198
  • 150
  • 472
  • 707
-1

Try opening the file in write-binary mode by using fw=open('file','wb').

If that doesn't work, try fw=open('file',mode='w',encoding='utf-8') to open in write mode with utf-8 encoding, which should solve your problem.

Valkyrie
  • 801
  • 8
  • 20