当前位置:首页>维修大全>综合>

怎样在Shell中把utf8编码转成中文显示

怎样在Shell中把utf8编码转成中文显示

更新时间:2023-09-15 21:17:41

怎样在Shell中把utf8编码转成中文显示

目前shell不支持unicode转义的字符串,bash4.2 alpha支持可行的办法就是做字符替换,给个Python的demo,面向行处理#!/usr/bin/env python# -*- coding: utf-8 -*-import reimport sys def main():for line in sys.stdin: sys.stdout.write(re.sub(r'\uw{4}', lambda e: unichr(int(e.group(0)[2:], 16)).encode('utf-8'), line)

)if __name__ == '__main__': main()复制代码用法:脚本保存为test.py,执行sumomos-MacBook-Pro:py lgr$ echo 'u9ED8u8BA4' | python ./test.py 默认

更多栏目