python从停用词txt文件中读取停用词到列表中


在读取停用此列表时遇到这行代码,记录理解过程:

#读取停顿词列表
stopword_list = [k.strip() for k in open('stopwords.txt', encoding='utf8').readlines() if k.strip() != '']

这一行代码有点长,用到的python知识点有:列表生成式、readlines和strip(),下文依次介绍。

列表生成式语法

[expr for iter_var in iterable] 
[expr for iter_var in iterable if cond_expr]

例子:

# -*- coding: UTF-8 -*-
lsit1=[x * x for x in range(1, 11)]
print(lsit1)

结果为

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

文件读取readlines

readlines()方法读取整个文件所有行,保存在一个列表(list)变量中,每行作为一个元素,但读取大文件会比较占内存。

f = open("a.txt")
lines = f.readlines()
print(type(lines))
for line in lines:
	print line
f.close()

结果为:

<type 'list'>
Hello
Welcome
What is the fuck...

str.strip()字符串处理函数

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。

注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。

strip() 处理的时候,如果不带参数,默认是清除两边的空白符,例如:/n, /r, /t, ’ ')。

再回头看这行代码就可以读懂了。
参考博客:
https://www.runoob.com/python3/python3-string-strip.html
https://blog.csdn.net/AliceGoToAnother/article/details/79119049

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用类似于上面的代码进行批量处理。具体步骤如下: 1. 导入需要的库 ```python import os import jieba from nltk.corpus import stopwords ``` 2. 加载停用词表 ```python stopwords = stopwords.words('chinese') ``` 3. 定义处理函数 ```python def process_file(input_file, output_file): # 读取文本数据 with open(input_file, 'r', encoding='utf-8') as fin: text = fin.read() # 使用jieba分词进行分词操作 words = jieba.cut(text) # 去除停用词 result = [] for word in words: if word not in stopwords: result.append(word) # 将结果写入新的文件 with open(output_file, 'w', encoding='utf-8') as fout: fout.write(' '.join(result)) ``` 该函数接受两个参数:输入文件和输出文件。函数内部使用`jieba.cut()`函数进行分词操作,并去除停用词,然后将结果写入新的文件。 4. 遍历文件夹并进行处理 ```python # 定义输入和输出文件夹 input_folder = '/path/to/input/folder' output_folder = '/path/to/output/folder' # 遍历文件的所有文件 for filename in os.listdir(input_folder): # 构造输入和输出文件的路径 input_file = os.path.join(input_folder, filename) output_file = os.path.join(output_folder, filename) # 处理文件 process_file(input_file, output_file) ``` 该代码段,需要将`/path/to/input/folder`替换为实际的输入文件夹路径,将`/path/to/output/folder`替换为实际的输出文件夹路径。然后使用`os.listdir()`函数遍历输入文件的所有文件,构造输入和输出文件的路径,并调用`process_file()`函数进行处理。 完整代码如下: ```python import os import jieba from nltk.corpus import stopwords # 加载停用词stopwords = stopwords.words('chinese') # 定义处理函数 def process_file(input_file, output_file): # 读取文本数据 with open(input_file, 'r', encoding='utf-8') as fin: text = fin.read() # 使用jieba分词进行分词操作 words = jieba.cut(text) # 去除停用词 result = [] for word in words: if word not in stopwords: result.append(word) # 将结果写入新的文件 with open(output_file, 'w', encoding='utf-8') as fout: fout.write(' '.join(result)) # 定义输入和输出文件夹 input_folder = '/path/to/input/folder' output_folder = '/path/to/output/folder' # 遍历文件的所有文件 for filename in os.listdir(input_folder): # 构造输入和输出文件的路径 input_file = os.path.join(input_folder, filename) output_file = os.path.join(output_folder, filename) # 处理文件 process_file(input_file, output_file) ``` 注意,上面的代码使用的是`utf-8`编码读写文件,如果输入文件的编码不是`utf-8`,需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值