byte-order-mark

Python read csv – BOM embedded into the first key

Python read csv – BOM embedded into the first key Question: I’m using Python 2.7.12. With this code snippet I’m saving a utf-8 csv file. I wrote the BOM (byte order mark) at the beginning of the file. import codecs import csv outputFile = open(“test.csv”, “wb”) outputFile.write(codecs.BOM_UTF8) fieldnames = [“a”, “b”] writer = csv.DictWriter(outputFile, fieldnames, …

Total answers: 2

Convert UTF-8 with BOM to UTF-8 with no BOM in Python

Convert UTF-8 with BOM to UTF-8 with no BOM in Python Question: Two questions here. I have a set of files which are usually UTF-8 with BOM. I’d like to convert them (ideally in place) to UTF-8 with no BOM. It seems like codecs.StreamRecoder(stream, encode, decode, Reader, Writer, errors) would handle this. But I don’t …

Total answers: 7

Write to UTF-8 file in Python

Write to UTF-8 file in Python Question: 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. …

Total answers: 8