Re: [問題] 一行字串中如何找出第一個中文字
I'm not sure what's the encoding of your string, so I made two programs.
Use at your own risk. Especially the second pattern is not exactly the range of
Chinese characters.
refs:
https://zh.wikipedia.org/wiki/big5
https://zh.wikipedia.org/wiki/UTF-8
https://en.wikipedia.org/wiki/CJK_Unified_Ideographs
----------------------------------------------------------
# -*- coding: big5 -*-
import re
s = "x=643 y=17 text=回首頁"
m = re.search('[\x81-\xfe][\x40-\x7e\xa1-\xfe]', s)
print m.group(0)
----------------------------------------------------------
# -*- coding: utf-8 -*-
import re
s = "x=643 y=17 text=回首頁"
m = re.search('([\xc0-\xdf][\x80-\xbf]{1})|([\xe0-\xef][\x80-\xbf]{2})|([\xf0-\xf7][\x80-\xbf]{3})', s)
print m.group(0)
#print m.group(0).decode("utf-8").encode("big5")
※ 引述《pandadao (panda)》之銘言:
: 各位前輩好,最近小弟在練習寫python,遇到一個難題,假設我現在有一行
: 文字,類似像 x=643 y=17 text=回首頁,可是有時候text後面會有一些符號
: 像是括號或逗點之類的,像是x=643 y=17 text=(回首頁) 要怎麼樣才能在
: 保留前面座標軸資料的前提下找出第一個中文字呢?麻煩高手教教我,謝謝
: 我目前是用python2
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.72.92.133
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1437574054.A.7C7.html
推
07/24 06:30, , 1F
07/24 06:30, 1F
推
07/26 17:24, , 2F
07/26 17:24, 2F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
Python 近期熱門文章
PTT數位生活區 即時熱門文章