Python write utf 8 string to file

I'm really confused with the codecs.open function. When I do:

file = codecs.open("temp", "w", "utf-8")
file.write(codecs.BOM_UTF8)
file.close()

It gives me the error

UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

If I do:

file = open("temp", "w")
file.write(codecs.BOM_UTF8)
file.close()

It works fine.

Question is why does the first method fail? And how do I insert the bom?

If the second method is the correct way of doing it, what the point of using codecs.open(filename, "w", "utf-8")?

dreftymac

30.2k26 gold badges115 silver badges177 bronze badges

asked Jun 1, 2009 at 9:42

John JiangJohn Jiang

10.5k11 gold badges49 silver badges60 bronze badges

3

I believe the problem is that codecs.BOM_UTF8 is a byte string, not a Unicode string. I suspect the file handler is trying to guess what you really mean based on "I'm meant to be writing Unicode as UTF-8-encoded text, but you've given me a byte string!"

Try writing the Unicode string for the byte order mark (i.e. Unicode U+FEFF) directly, so that the file just encodes that as UTF-8:

import codecs

file = codecs.open("lol", "w", "utf-8")
file.write(u'\ufeff')
file.close()

(That seems to give the right answer - a file with bytes EF BB BF.)

EDIT: S. Lott's suggestion of using "utf-8-sig" as the encoding is a better one than explicitly writing the BOM yourself, but I'll leave this answer here as it explains what was going wrong before.

Python write utf 8 string to file

Zanon

26.8k20 gold badges109 silver badges122 bronze badges

answered Jun 1, 2009 at 9:46

Jon SkeetJon Skeet

1.4m831 gold badges8975 silver badges9086 bronze badges

5

It is very simple just use this. Not any library needed.

with open('text.txt', 'w', encoding='utf-8') as f:
    f.write(text)

answered Aug 12, 2021 at 11:17

Python write utf 8 string to file

Kamran GasimovKamran Gasimov

1,0531 gold badge12 silver badges11 bronze badges

@S-Lott gives the right procedure, but expanding on the Unicode issues, the Python interpreter can provide more insights.

Jon Skeet is right (unusual) about the codecs module - it contains byte strings:

>>> import codecs
>>> codecs.BOM
'\xff\xfe'
>>> codecs.BOM_UTF8
'\xef\xbb\xbf'
>>> 

Picking another nit, the BOM has a standard Unicode name, and it can be entered as:

>>> bom= u"\N{ZERO WIDTH NO-BREAK SPACE}"
>>> bom
u'\ufeff'

It is also accessible via unicodedata:

>>> import unicodedata
>>> unicodedata.lookup('ZERO WIDTH NO-BREAK SPACE')
u'\ufeff'
>>> 

tzot

88.9k29 gold badges135 silver badges200 bronze badges

answered Jun 1, 2009 at 10:12

gimelgimel

80k10 gold badges73 silver badges104 bronze badges

0

I use the file *nix command to convert a unknown charset file in a utf-8 file

# -*- encoding: utf-8 -*-

# converting a unknown formatting file in utf-8

import codecs
import commands

file_location = "jumper.sub"
file_encoding = commands.getoutput('file -b --mime-encoding %s' % file_location)

file_stream = codecs.open(file_location, 'r', file_encoding)
file_output = codecs.open(file_location+"b", 'w', 'utf-8')

for l in file_stream:
    file_output.write(l)

file_stream.close()
file_output.close()

answered Feb 8, 2012 at 20:35

RicardoRicardo

5988 silver badges11 bronze badges

2

python 3.4 >= using pathlib:

import pathlib
pathlib.Path("text.txt").write_text(text, encoding='utf-8') #or utf-8-sig for BOM

answered Apr 8 at 20:52

Python write utf 8 string to file

celsowmcelsowm

5908 gold badges30 silver badges56 bronze badges

If you are using Pandas I/O methods like pandas.to_excel(), add an encoding parameter, e.g.

pd.to_excel("somefile.xlsx", sheet_name="export", encoding='utf-8')

This works for most international characters I believe.

answered Dec 8, 2021 at 12:04

Not the answer you're looking for? Browse other questions tagged python utf-8 character-encoding byte-order-mark or ask your own question.

How do I save a UTF

“python save as utf-8” Code Answer's.
import codecs..
file = codecs. open("lol", "w", "utf-8").
file. write(u'\ufeff').
file. close().

How do I create a UTF

Step 1- Open the file in Microsoft Word. ... .
Step 2- Navigate to File > Save As..
Step 3- Select Plain Text. ... .
Step 4- Choose UTF-8 Encoding..

How do you write encoding in Python?

encode method does, and the result of encoding a unicode string is a bytestring (a str type.) You should either use normal open() and encode the unicode yourself, or (usually a better idea) use codecs. open() and not encode the data yourself. Show activity on this post.

How do I change a text file to UTF

“convert file encoding to utf-8 python” Code Answer.
with open(ff_name, 'rb') as source_file:.
with open(target_file_name, 'w+b') as dest_file:.
contents = source_file. read().
dest_file. write(contents. decode('utf-16'). encode('utf-8')).