我有一个文本文件,每行开头都有一个中文字符x3400-9FFF。我想用它的十进制(而不是十六进制)代码替换它,而不影响文件的其余部分,即ASCII。
在Python中使用ord()函数应该非常简单,对吧?我就是不懂什么Python。下面是我写的内容:
import sublime
import sublime_plugin
#converts Chinese to decimal
class HanCommand(sublime_plugin.TextCommand):
def replaceHanzi(self, edit, text):
return re.sub(r'([\x3400-\x9FFF])', ord())
def run(self, edit):
self.replaceHanzi(edit, text)发布于 2020-05-05 00:44:35
尝试使用replace()函数:
s = "א aaaaabbb"
s = s.replace(u"א", str(ord(u"א")))
print(s)输出:
1488 aaaaabbb1488是此字母的Unicode值,以10为基数。
另外,请注意,在
你不能使用edit和text。您应该按如下方式使用它们:
def replaceHanzi(self, edit, text):
return text.replace(edit, str(ord(edit)))https://stackoverflow.com/questions/61597043
复制相似问题