Python Forum
Writing chr(128) to text file returns errors.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing chr(128) to text file returns errors.
#1
I am trying to write characters to a file.

My problem is that for for N=128, file.write(chr(N)) fails and returns error: UnicodeEncodeError: 'charmap' codec can't encode character '\x80'.

I think it is very weird that something that is allowed in print() would return an error in file.write(). However for N=255 it does not return an error, so it is not simply that it fails for all N>127.

The whole code for this example is:

    N=128

    print(chr(N)) #This works just fine.

    file=open('output.txt','w')
    file.write(chr(N))  #This one returns errors!
    file.close() 
    
I also tried utf-8, but with no luck.

    N=128

    print(chr(N)) #This works just fine.

    file=open('output.txt','w',encoding="utf-8")
    file.write(chr(N)) #No error but output.txt is empty!
    file.close()
It gives no errors, but the output file is empty.

My python version is Anaconda 3.6.5 and running on windows 7.
Reply
#2
After a bit of research I have discovered that chr 128 is what is known as a padding character. Displaying the character depends on the font which is why I was seeing two different characters. This special character is a Unicode control character and depending on the font setup it will be displayed differently as there is no preset standard. The chr is U+0080 and its UTF-8 encoding is: b'\xc2\x80.

That being said I am not receiving any errors from your first or second code snipit (both should work). It also explains why your not seeing anything in your text file for the second code snipit (its like magic, you have to believe its there) trust me its there.
Reply
#3
use json file instead of txt
import json
filename = 'user.json'
n=128
with open(filename, 'w')as file_object:
json.dump(n,file_object)
if you want to use txt file so do like this
filename = 'programming.txt'
n=128
with open(filename, 'w')as file_object:
file_object.write(str(n))
Reply
#4
(Aug-06-2018, 05:33 PM)Vysero Wrote: After a bit of research I have discovered that chr 128 is what is known as a padding character. Displaying the character depends on the font which is why I was seeing two different characters. This special character is a Unicode control character and depending on the font setup it will be displayed differently as there is no preset standard. The chr is U+0080 and its UTF-8 encoding is: b'\xc2\x80.

That being said I am not receiving any errors from your first or second code snipit (both should work). It also explains why your not seeing anything in your text file for the second code snipit (its like magic, you have to believe its there) trust me its there.

Why are you not getting error in the first snippet? Could it have something to do with packages or version?
The second snippet has a very weird result. Not only is the output file empty, but if you afterwards read the output file in again it contains TWO characters. "The chr is U+0080 and its UTF-8 encoding is: b'\xc2\x80." Is it two characters because there are two backslashes?

If I do the same thing in C++, it works without errors and the output file shows a € sign. (That is with default settings, no encoding specified.) I was hoping for a similar result with Python.
Reply
#5
(Aug-07-2018, 10:33 AM)Qwert_Yuiop Wrote: Why are you not getting error in the first snippet?

Well I went ahead and tried both code snippets again, this time on a windows machine and once again they both worked. Inherently, this doesn't surprise me because there is nothing wrong with the code itself. The error you got from attempting to run the first bit of code suggests to me that your working with an older version of Python. Are you by chance working with Python 2.x?
Reply
#6
(Aug-07-2018, 10:33 AM)Qwert_Yuiop Wrote: If I do the same thing in C++, it works without errors and the output file shows a € sign.
Then it may use encoding latin-1
with open('foo1.txt', 'w', encoding='latin-1') as f:
    f.write(f'foo {chr(128)} bar')
Output:
foo € bar
As python 3 has full Unicode support so all text in Python 3 is Unicode,then can just paste in euro sign €.
with open('foo1.txt', 'w', encoding='utf-8') as f_out:
    f_out.write(f'foo € bar')

with open('foo1.txt', encoding='utf-8') as f:
    print(f.read())
Output:
foo € bar
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 815 Feb-16-2023, 08:11 PM
Last Post: cubangt
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,124 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,377 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing into 2 text files from the same function paul18fr 4 1,678 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Writing to json file ebolisa 1 1,006 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,669 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Writing to External File DaveG 9 2,490 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,971 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Writing to file ends incorrectly project_science 4 2,691 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,349 Dec-23-2020, 08:04 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020