การ encode string cp874 => utf-8

คือพอดีผมจะค้นหาตำแหนงของตัวอักษร แล้วผมมีปัญหาว่าค้นหาไม่เจอเพราะ
ชุดอักขระที่ใช้ค้นหา encode ด้วย utf-8
แต่ชุดอักขระที่ถูกค้นหา encode ด้วย cp874 ทำให้หาไม่เจอคับ
ในไฟล์ code ของผม ผมเพิ่ม
# -*- coding: utf-8 -*- (encode ตัวไฟล์เป็น utf-8 ด้วย)
แล้ว ข้อมูลที่จะค้นหาเป็น cp874 ผมจะ encode เป็น utf-8
แล้ว error ดังนี้
1. string.decode('utf8')
Error:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa4 in position 4478: unexpected code byte
[0xa4 = ค]
2. unicode(string)
Error:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

จะแก้ไขอย่างไรดีครับ

sugree's picture

น่าจะเป็นแบบนี้นะ

def search(text, s): # text เป็น utf-8 s เป็น cp874
    return text.find(s.decode('cp874'))
khao_lek's picture

ขอบคุณครับ

khao_lek's picture

ยังไม่ได้เลยครับ เฮ้อผ่านได้ก็ไปไกล้แล้วอะคับ
ติดอยู่แต่ตรงนี้ไปไหนไม่ได้เลยคับ
ผมลองแก้ไข code ของคุณ sugree หลายๆ แบบแล้วก็ไม่ได้ครับ
ผมลอง print type ออกมานะครับ
ครั้งแรก จะได้ <type 'str'>
ครั้งที่2จะได้ <type 'unicode'>
code

def search(text, s):
    sText = u''
    sText = text.decode('utf8')
    return sText.rfind(s)
#response.get_data() ได้มาจาก mechanize
strDataUrl = response.get_data()
print type(strDataUrl)
strDataUrl_II = unicode(strDataUrl,'cp874')
print type(strDataUrl_II)
strFind = u'ขายออก</strong></span></span></th>'
substrDataUrl = search(strDataUrl_II,strFind)
strSubData = strDataUrl_II[substrDataUrl:]
print strSubData

error

error in 
substrDataUrl = search(strDataUrl_II,strFind)
 
error in 
sText = text.decode('utf8')
 
'ascii' codec can't encode characters in position 4478-4484: ordinal not in range(128)
sugree's picture

ผมไปลองมาให้เรียบร้อยแล้วครับ

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys
sys.path.insert(0, 'mechanize-0.1.7b')
sys.path.insert(0, 'ClientForm-0.2.7')
from mechanize import Browser
 
def search(text, s):
    return text.rfind(s)
 
br = Browser()
br.set_handle_robots(False)
response = br.open('http://www.manager.co.th/home/default.html')
rawdata = response.get_data()
print type(rawdata)
 
ucdata = rawdata.decode('cp874')
print type(ucdata)
 
s = u'เจ็ด'
index = search(ucdata, s)
 
match = ucdata[index:index+30]
print match
khao_lek's picture

ผมเอา code ของคุณ sugree มาสร้างไฟล์ test แล้วก็เป็นเหมือนเดิม
นะครับ มันจะแตกต่างกับคุณ sugree ตรงไหนครับ หากไม่ print match
ก็ไม่เป็นไรนะคับ และก็เอา match ไปใช้อะไรไม่ได้เลยครับ เขียนลง
ไฟล์ก็ error เอามาใช้งานต่อก็ error ครับ
os : windows
python : 2.5

<type 'str'>
<type 'unicode'>
Traceback (most recent call last):
  File "D:\myWorkEclipse\GUI\rpt\test.py", line 24, in <module>
    print match
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
sugree's picture

ฮ่ะๆ วินโดส์นี่เอง ถ้าเป็นวินโดส์ตอน print ต้องแบบนี้

print match.encode('tis-620')

khao_lek's picture

ได้แล้วครับขอบคุณมากเลยครับคุณ sugree

ย้าย Codenone

ประกาศย้าย Codenone ไปใช้ Forum ของ Blognone แทนครับ ตามไปตั้งกระทู้ต่อได้ที่ Codenone Forum (รายละเอียดอ่านจากกระทู้ ย้าย Codenone ไปรวมกับ Blognone)

กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น