Tags:
Forums: 

พอดีผมรับค่าข้อมูล json มาเก็บไว้ใน DB แต่ใน JSON นั้นมีข้อมูลภาษาไทยด้วยในรูปแบบ เช่น \u0e15\u0e33\u0e41\u0e2b\u0e19\u0e48\u0e07\u0e17\u0e35\u0e48\u0e15\u0e31\u0e49\u0e07 ซึ่งพอเก็บใน ฐานข้อมูลตัว \ (backslash) มันหายไปเช่น u0e15u0e33u0e41u0e2bu0e19u0e48u0e07u0e17u0e35u0e48u0e15u0e31u0e49u0e07 จะมีวิธีใดบ้างให้ตัว \ (backslash) กลับมาในแบบต้นฉบับครับ หรือฟังชั่นที่เรียกใช้งานครับ

python : 3.6
db: mysql5

Get latest news from Blognone
By: icez
ContributoriPhoneAndroidRed Hat
on 31 January 2018 - 16:43 #1031483

แสดงว่าตอนเก็บข้อมูลไม่ได้ escape ให้ดีครับมันเลยหาย ลองซ่อมด้วย regexp ไม่ก็ search/replace ดูครับ

By: khao_lek on 1 February 2018 - 09:06 #1031604 Reply to:1031483

ขอบคุณมากครับสำหรับคำแนะนำ พอดีตอนเก็บ ผมเก็บเป็น json ทั้งก้อนเลย จาก ws แบบต้นฉบับเลย เช่น

{"service":[{"id":10234567890,"job":{"text":"การทำงาน","id":1234567890},"token":"afasadfoaskfasjfsolsafkasofsk"}]}

อย่างนี้ผมต้อง escape ทั้งชุด หรือเฉพาะ text อย่างเดี่ยวครับ

By: icez
ContributoriPhoneAndroidRed Hat
on 1 February 2018 - 10:19 #1031635 Reply to:1031604

ใช้ prepared statement ตอน insert สิครับ... ปลอดภัยค่อนข้างชัวร์

By: kora on 8 February 2018 - 15:46 #1032728

import re
s = 'u0e15u0e33u0e41u0e2bu0e19u0e48u0e07u0e17u0e35u0e48u0e15u0e31u0e49u0e07'
s = re.sub(r'u([0-9a-f]{4})', lambda m: chr(int(m[1], 16)), s)
print(s) # 'ตำแหน่งที่ตั้ง'

By: khao_lek on 12 March 2018 - 14:23 #1037873

ผมแก้ไขได้แล้วครับ ผมใช้งาน bytes.fromhex() กับ string.hex() ครับ ขอบคุณมากครับ