导航:首页 > 编程语言 > python清除所有非汉字

python清除所有非汉字

发布时间:2022-04-20 10:57:41

python怎么删除包含指定中文的行

#!/usr/bin/envpython
#coding=utf-8

defread_del_list(path):
del_list=list()
withopen(path,'w')asfile_handle:
forrowinfile_handle:
del_list.append(row.strip())
returndel_list

deffilte_file(from_file,to_file,del_list):
withopen(from_file)asfile_handle_from:
withopen(to_file)asfile_handle_to:
forrowinfile_handle_from:
ifnotany(key_wordinrowforkey_wordindel_list):
file_handle_to.write(row)

if__name__=='__main__':
del_list=read_del_list(r"del_list.txt")#读取过滤规则
filte_file(r"source.txt","output.txt",del_list)#过滤文件

② python字符串如何去掉英文字母以外的字符

可以利用正则表达式来去除

既然说到了字符串的操作,那么就目前而言是没有别的方法会比正则表达式更加方便的:

正则表达式中代表非字母的写法如下:

[^a-zA-Z]

#code:

③ python3 如何去除字符串中不想要的字符

去除不想要的字符有很多种方法:

1、利用python中的replace()方法,把不想要的字符替换成空;

2、利用python的rstrip()方法,lstrip()方法,strip()方法去除收尾不想要的字符。

用法如下:

Python3 replace()方法


Python3 rstrip()方法

Python3 lstrip()方法

④ 用C程或python去除文件中的除",""."外的符号,只留下汉字

# -*- coding: cp936 -*-

with open("out.txt") as file:
import string
import re
s = re.sub("(,|\.)","",string.punctuation) + u"《》"
s = "[%s]" % s
out = re.sub(s,"",rece(str.__add__,file.readlines()).decode('GB2312'))
with open("res.txt","w") as file:
file.write(out.encode('GB2312'))

不能消除"\"字符以及"《》",需要的话修改就行

⑤ python如何去除字符串中不想要的字符

问题:
过滤用户输入中前后多余的空白字符
‘ ++++abc123--- ‘
过滤某windows下编辑文本中的’\r’:
‘hello world \r\n’
去掉文本中unicode组合字符,音调
"Zhào Qián Sūn Lǐ Zhōu Wú Zhèng Wáng"
如何解决以上问题?
去掉两端字符串: strip(), rstrip(),lstrip()

123456789101112131415

#!/usr/bin/python3 s = ' -----abc123++++ ' # 删除两边空字符print(s.strip()) # 删除左边空字符print(s.rstrip()) # 删除右边空字符print(s.lstrip()) # 删除两边 - + 和空字符print(s.strip().strip('-+'))

删除单个固定位置字符: 切片 + 拼接

123456

#!/usr/bin/python3 s = 'abc:123'# 字符串拼接方式去除冒号new_s = s[:3] + s[4:]print(new_s)

删除任意位置字符同时删除多种不同字符:replace(), re.sub()

1234567891011

#!/usr/bin/python3 # 去除字符串中相同的字符s = '\tabc\t123\tisk'print(s.replace('\t', '')) import re# 去除\r\n\t字符s = '\r\nabc\t123\nxyz'print(re.sub('[\r\n\t]', '', s))

同时删除多种不同字符:translate() py3中为str.maketrans()做映射

1234567

#!/usr/bin/python3 s = 'abc123xyz'# a _> x, b_> y, c_> z,字符映射加密print(str.maketrans('abcxyz', 'xyzabc'))# translate把其转换成字符串print(s.translate(str.maketrans('abcxyz', 'xyzabc')))

去掉unicode字符中音调


#!/usr/bin/python3 import sysimport unicodedatas = "Zhào Qián Sūn Lǐ Zhōu Wú Zhèng Wáng"remap = { # ord返回ascii值 ord('\t'): '', ord('\f'): '', ord('\r'): None }# 去除\t, \f, \ra = s.translate(remap)'''通过使用dict.fromkeys() 方法构造一个字典,每个Unicode 和音符作为键,对于的值全部为None然后使用unicodedata.normalize() 将原始输入标准化为分解形式字符sys.maxunicode : 给出最大Unicode代码点的值的整数,即1114111(十六进制的0x10FFFF)。unicodedata.combining:将分配给字符chr的规范组合类作为整数返回。 如果未定义组合类,则返回0。'''cmb_chrs = dict.fromkeys(c for c in range(sys.maxunicode) if unicodedata.combining(chr(c))) #此部分建议拆分开来理解b = unicodedata.normalize('NFD', a)'''调用translate 函数删除所有重音符'''print(b.translate(cmb_chrs))

⑥ 如何用python将列表中非字符串部分删掉 我有一个列表例如[“我”,“的”,0,“程序”],请

s=["我","的",0,"程序"]
s=[value for value in s if type(value)==type("")]

⑦ Python给定一个字符串,去除字符串的非字母字符然后将每个字符串的首字母大写

inp = input()
inp2 = ''
for i in inp:
if i.isalpha():
inp2 += i
print(inp2.upper())

⑧ python删除特定文字下面的所有内容并保存

初学就要多查找相关资料,然后自己尝试写代码,在改错中进步:
思路:
先建立一个临时文件(用open即可),
然后顺序读取txt文件的每一行(open,readline,用 while循环),
判断读取的那一行是否是abcdefg,不是就保存到临时文件,是的话就结束循环。
关闭文件,然后可以把原来的txt文件删除,把临时文件更名为txt。(import os,用os操作)

⑨ python 如何去掉字符串中特定的字符

参考以下:

In [20]: aa=u\\'kasdfjskdf12334342\\'

In [21]: filter(str.isdigit,str(aa))
Out[21]: \\'12334342\\'

In [22]: filter(str.isalpha,str(aa))
Out[22]: \\'kasdfjskdf\\'

注意,这个因为要用到 str 函数,所以如果字符串中有非 ascii 码(如汉字)会报错。
要先去掉非 ascii 码字符再用上面的方法。

⑩ Python中如何删除列表中包含中文的元素

首先
代码开头 加上
#-*- coding:gbk -*-

然后
list.remove('王五')
这样去删除

阅读全文

与python清除所有非汉字相关的资料

热点内容
程序员怼主管电影 浏览:416
cadpdf打印的 浏览:157
柴油无pdf 浏览:329
科技时代编程教学 浏览:106
php客户端地址服务器地址 浏览:518
php动漫源码 浏览:919
络程序员 浏览:566
苹果手机app更新怎么暂停 浏览:695
命令法典在哪交 浏览:793
如何将软件附带文件夹 浏览:392
ppt转换pdf软件 浏览:707
phpjsondecode乱码 浏览:724
如何在服务器管理器添加角色 浏览:395
程序员那么可爱陆漓出车祸失忆了 浏览:138
51单片机ad转换教程 浏览:436
TPM怎么加密文件夹 浏览:81
找工作用什么app靠谱 浏览:915
从程序员转产品经理 浏览:793
linux查询序列号 浏览:251
vi输入命令 浏览:654