python3如何提取汉字

采用正则表达式的方法对字符串进行处理。

str1 = "{我%$是,《速$@.度\发》中 /国、人"

(1)提取汉字

汉字的范围为”\u4e00-\u9fa5“,这个是用Unicode表示的。

import re
res1 = ''.join(re.findall('[\u4e00-\u9fa5]',str1))
print(res1)

输出为:

‘我是速度发中国人’

(2)去除所有符号。采用清理数据,仅保留字母、数字、中文的方法。

import re
res1 = re.sub("[^a-zA-Z0-9\u4e00-\u9fa5]", '', str1) 
print(res1)

输出为:

‘我是速度发中国人’

相关推荐

  1. 如何使用Python提取文件名

    2024-04-22 00:26:05       35 阅读
  2. python如何提取html中所有中文

    2024-04-22 00:26:05       25 阅读
  3. Visual Studio Installer 运行python 汉字

    2024-04-22 00:26:05       28 阅读
  4. 如何使用Python提高图像分辨率?

    2024-04-22 00:26:05       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-22 00:26:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-22 00:26:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-22 00:26:05       82 阅读
  4. Python语言-面向对象

    2024-04-22 00:26:05       91 阅读

热门阅读

  1. jQuery 选择器有几种,分别是什么

    2024-04-22 00:26:05       33 阅读
  2. Linux系统的账号和权限管理

    2024-04-22 00:26:05       35 阅读
  3. 赠品:跳动的心

    2024-04-22 00:26:05       36 阅读
  4. ZYNQ-700呼吸灯

    2024-04-22 00:26:05       36 阅读
  5. 【设计模式】8、adapter 适配器模式

    2024-04-22 00:26:05       32 阅读
  6. 考古:MFC界面的自适应缩放(代码示例)

    2024-04-22 00:26:05       36 阅读
  7. Leetcode 104. 二叉树的最大深度

    2024-04-22 00:26:05       34 阅读