Having a bizarre issue running this UTF-8 encoded script in Python 3.2. Python refuses to run if it contains the Japanese hiragana character の (see stack trace below)
Traceback (most recent call last):
File "MyScript.py", line 20, in <module>
print(no)
File "C:\Python32\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u306e'
in position 0: character maps to <undefined>
It runs fine without this one single character (there are other characters in the file as well), and I'm at a loss to explain why. Any help would be appreciated.
Here is a script that reproduces the error for me:
#!/usr/bin/env python
# coding=utf-8
import glob
import codecs
import os.path
from datetime import datetime, timedelta
assTemplate = \
r"""タイトル\N {time.year}年{time.month}月{time.day}日 {age}\N{place}"""
for mtsName in glob.glob('./*.MTS'):
baseName = mtsName.lower().replace('.mts', '')
mtsName = os.path.abspath(mtsName)
# Get the time the video file was created.
mtsTimestamp = datetime.fromtimestamp(os.stat(mtsName).st_ctime)
no = '\u306e'
print(no) ## UnicodeDecodeError
age = '生後'
place = '自宅'
print('自宅')
# Generate the contents of the ASS file.
assContents = assTemplate.format(time=mtsTimestamp, age=age, place=place)
# Write the ASS file.
print(assContents)
The reason for using Python 3.2 this was that string formatting with unicode strings was not working at all for me in Python 2.7.2.