[PYTHON] # Function that returns the character code of a string

Function to find the character code of the argument msg


def get_jp_encoding_name (msg,char_code = 'iso-2022-jp'):
	import types
	
	enc_name = char_code or 'iso-2022-jp'
	check_type = ['utf_8',
				   enc_name,		'iso2022_jp_1', 'iso2022_jp_2',
				   'iso2022_jp_2004','iso2022_jp_3', 'iso2022_jp_ext',
				   'shift-jis',	  'cp932',		'shift_jis_2004',
				   'shift_jisx0213', 'euc_jp',	   'euc_jis_2004',
				   'euc_jisx0213']
	for enc in check_type:
		try:
			if isinstance(msg, types.StringType):
				s = msg.decode(enc)
			else:
				s = unicode(msg,enc)
		except :
			continue
		return enc
	return None

The code that was flowing in Python-ML at the time.

Recommended Posts

# Function that returns the character code of a string
Get the variable name of the variable as a character string.
A function that returns a random name
[Ansible] Example of playbook that adds a character string to the first line of the file
pandas Fetch the name of a column that contains a specific character
[Introduction to Python] How to split a character string with the split function
[Python3] Rewrite the code object of the function
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
A function that measures the processing time of a method in python
[Python] Get the character code of the file
A code that corrects the yoon / sokuon (sokuon)
[PowerShell] Get the reading of the character string
How to create a wrapper that preserves the signature of the function to wrap
The eval () function that calculates a string as an expression in python
[Introduction to Python] How to write a character string with the format function
[Python] Note: A self-made function that finds the area of the normal distribution
Get the caller of a function in Python
Convert the character code of the file with Python3
A story that reduces the effort of operation / maintenance
[Python] A program that counts the number of valleys
Cut a part of the string using a Python slice
Make a BOT that shortens the URL of Discord
Generate that shape of the bottom of a PET bottle
A story that analyzed the delivery of Nico Nama.
[Python] A program that compares the positions of kangaroos.
Calculate the product of matrices with a character expression?
A regular expression that finds N or more consecutive substrings of the same character
[Python] Leave only the elements that start with a specific character string in the array
Explain the code of Tensorflow_in_ROS
2.x, 3.x character code of python
A tool that automatically turns the gacha of a social game
Code that sets the default value in case of AttributeError
A string permutation code that uses an algorithm that generates permutations.
I made a function to check the model of DCGAN
I tried a little bit of the behavior of the zip function
Extract the value of dict or list as a string
A Python script that compares the contents of two directories
How to connect the contents of a list into a string
A server that returns the number of people in front of the camera with bottle.py and OpenCV
Create a bot that only returns the result of morphological analysis with MeCab on Discord
An example of a mechanism that returns a prediction by HTTP from the result of machine learning
How to quickly count the frequency of appearance of characters from a character string in Python?
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
A memo that reproduces the slide show (gadget) of Windows 7 on Windows 10.
When incrementing the value of a key that does not exist
Find the optimal value of a function with a genetic algorithm (Part 2)
Is it a character string operation?
A formula that simply calculates the age from the date of birth
A story that struggled to handle the Python package of PocketSphinx
[Django] A brief summary of the log output function so that even beginners can understand it.
If you give a list with the default argument of the function ...
Outputs a line containing the specified character string from a text file
[Python] Make the function a lambda function
The story of creating a site that lists the release dates of books
[Python3] Define a decorator to measure the execution time of a function
Python-dynamically call a function from a string
How to extract the desired character string from a line 4 commands
[Python] A function that searches the entire string with a regular expression and retrieves all matching strings.
I made a slack bot that notifies me of the temperature
A script that returns 0, 1 attached to the first Python prime number
Finding the optimum value of a function using a genetic algorithm (Part 1)