开发者

Cannot run script with one particular Unicode character in Python 3.2

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.


You are trying to print a unicode character to a terminal that uses cp1252. cp1525 does not support any Japanese characters at all. It is hence not a problem with that character, I bet you get the exact same error with any Japanese character.


i had this problem ,too. My language is Vietnamese. You can cut the file cp1252.py or delete this file. You should cut this file and move another folder , any folders you like. Now, in encodings folder don't have file cp1252.py , don't worry. Next you copy file utf-8 in encodings folder and paste this file to encodings folder and rename this file is cp1252.py . Do you understand.

I fixed this problem like that.

Success for you!

My yahoo nickname is: phong_ux . If you need more help , i am willing to help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜