When I open the file in Python, the error “UnicodeDecodeError: ‘cp932’ codec can’t decode byte 0x99 in position ~ : illegal multibyte sequence” occurred.
The code is as below.
path = 'wordpress/wp-admin/about.php'
with open(path) as f:
lines = f.readlines()
print(lines)
Environment
Windows 10 Python 3.8.6
The cause of this problem
The character code ‘cp932’ is Microsoft Cade page 932(Shift_JIS). And ‘0x99’ is the number of 1byte represented by hexadecimal. This error occurs when the character code used for decoding the file does not match the character code of the file.
Solution
Solution 1. Use encoding option argument
If you know the coding of the file, you can specify it with option argument of open() function.
with open(path, encoding = "utf_8") as f:
lines = f.readlines()
print(lines)
Solution 2. Use try and except
If you’d like to use a few character coding and ignore files of other character coding.
try:
with open(path, encoding = "shift_jis") as f:
lines = f.readlines()
print(lines)
except:
pass
try:
with open(path, encoding = "ascii") as f:
lines = f.readlines()
print(lines)
except:
pass
try:
with open(path, encoding = "utf_8") as f:
lines = f.readlines()
print(lines)
except:
pass
Solution 3. Use chardet
Chardet is a module to detect the character encoding.
get_the_category(); is the function to get the categories of “current” post or page. This page is index.php with no category, so it return “Uncategorized”.
At the last time, in “Create Original Theme in WordPress 1“, I created the simplest minimum theme “techblog”. I’ll create practical WordPress theme from now on.
GOAL
To design the configuration of the WordPress site.
the configuration of pages
This is the page transition diagram. The top page is home. Pages “Home”, “About”, “Links” and “Posts(Post List)” can be accessed directly with main menu.
This is the construction of each page and links among them.
Detail design of each page
Home page
This page is used for “Home”.
Posts page
This page is used for “Posts (all posts)”, the result searched by categories and the result searched by words.
Single Page
This page is used for “About”, “Links” and the page for each single posts.
Side bar
Some of these items can be added easily by using Plugins.
style.css is the style sheet for the site. style.css should have infromation about the theme. Please check “Main Stylesheet (style.css)” in wordpress.org for details.
/*
Theme Name: Techblog
Theme URI: #
Author: Nako
Author URI: https://s-nako.work
Description: This is the simplest theme for engineer's technical blog
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: techblog
Domain Path: /languages/
Tags: blog, news,
This theme, like WordPress, is licensed under the GPL.
*/
body {
color: #6B6B6B;
background-color:#DADADA;
font-family: 'Open Sans', sans-serif;
line-height: 1.7;
font-size: 14px;
}
The information in style.css is displayed in Appearance >Themes.
functions.php
functions.php is function set for the site. I don’t use any original function at this time.
<?php
/**
* techblog functions and definitions
* @package Techblog
* @since Techblog 1.0
*/
if ( ! defined( 'TECHBLOG_VERSION' ) ) {
// Replace the version number of the theme on each release.
define( 'TECHBLOG_VERSION', '1.0.0' );
}
Install the created theme
Put the directory where index.php, style.css and functions.php is into the themes directory “xampp\htdocs\wordpress\wp-content\themes”.
You can activate the theme you created in the Appearance >Themes.
I’d like to create my original theme for WordPress. This is the preparation for development of WordPress in XAMPP.
GOAL
To create develop environment for WordPress development in XAMPP.
Environment
Windows10 XAMPP 7.4.10
If you don’t have XAMPP, please install it first according to “Installation of XAMPP“. And If you’ll use MariaDB in a production environment, make it secure by setting password according to “How To Set MySQL Password in XAMPP“.