[PYTHON] Omit BOM from the beginning of the string

BOM (Byte Order Mark) You should die. There is no mercy.

What is a BOM?

This is [Wikipedia](https://ja.wikipedia.org/wiki/%E3%83%90%E3%82%A4%E3%83%88%E3%82%AA%E3%83%BC%E3 % 83% 80% E3% 83% BC% E3% 83% 9E% E3% 83% BC% E3% 82% AF).

Why do you erase it?

If you use csv.DictReader or something, BOM will be added to the beginning of the header, so if you think that you will import it with seq on the first line, you will end up with a header like <0xEF> seq.

How do you erase it?

--I think you can erase it with nkf. --You may delete it on the program side.

Erase on the command line

$ nkf --overwrite -oc=UTF-8 filename

I think this is the royal road. There is nothing wrong with erasing it before reading it.

Erase on the application side

Because it is not always possible to erase it before importing.

import codecs
def strip_bom(s):
    s = s.encode('utf8')
    if s.startswith(codecs.BOM_UTF8):
        return s[len(codecs.BOM_UTF8):].decode('utf8')
    return s.decode('utf8')

The codecs module has a constant called BOM_UTF8, but why can't I erase it with the ʻopen` option?

Recommended Posts

Omit BOM from the beginning of the string
Learning notes from the beginning of Python 1
Learning notes from the beginning of Python 2
The beginning of cif2cell
Finding the beginning of Abenomics from NT magnification 2
Finding the beginning of Abenomics from NT magnification 1
Learn Nim with Python (from the beginning of the year).
Study from the beginning of Python Hour1: Hello World
Mathematical understanding of principal component analysis from the beginning
Study from the beginning of Python Hour8: Using packages
DJango Note: From the beginning (simplification and splitting of URLConf)
First Python 3 ~ The beginning of repetition ~
DJango Memo: From the beginning (preparation)
Existence from the viewpoint of Python
Carefully derive the interquartile range of the standard normal distribution from the beginning
DJango Memo: From the beginning (model settings)
[Understanding in 3 minutes] The beginning of Linux
Shout Hello, Reiwa! At the beginning of Reiwa
DJango Note: From the beginning (form processing)
[PowerShell] Get the reading of the character string
Get the contents of git diff from python
DJango Memo: From the beginning (creating a view)
Divide the string into the specified number of characters
Change the decimal point of logging from, to.
Extract only complete from the result of Trinity
DJango Memo: From the beginning (Error screen settings)
From the introduction of pyethapp to the execution of contract
The transition of baseball as seen from the data
The story of moving from Pipenv to Poetry
Summary from the beginning to Chapter 1 of the introduction to design patterns learned in the Java language
The story of launching a Minecraft server from Discord
The wall of changing the Django service from Python 2.7 to Python 3
Get the variable name of the variable as a character string.
Used from the introduction of Node.js in WSL environment
Calculate volume from the two-dimensional structure of a compound
[GoLang] Set a space at the beginning of the comment
[Python] Get the text of the law from the e-GOV Law API
Cut a part of the string using a Python slice
Kaggle competition process from the perspective of score transitions
The idea of Tensorflow learned from potato chip manufacturing
Get the return code of the Python script from bat
Python points from the perspective of a C programmer
# Function that returns the character code of a string
DJango Note: From the beginning (using a generic view)
DJango Note: From the beginning (creating a view from a template)
Othello ~ From the tic-tac-toe of "Implementation Deep Learning" (4) [End]
Visualize the number of complaints from life insurance companies
I tried to summarize the string operations of Python
[Note] Beginning of programming
The meaning of self
the zen of Python
The story of sys.path.append ()
How to quickly count the frequency of appearance of characters from a character string in Python?
Summary of string operations
Revenge of the Types: Revenge of types
Obtain the sequence information of the translated protein from the mutation information of CDS
Optimal measurement plan --From the October issue of the OR magazine
Find out the apparent width of a string in python
Examine the margin of error in the number of deaths from pneumonia
Analyzing user dissatisfaction very easily from the contents of inquiries
Get UNIXTIME at the beginning of today with a command