网站首页 > 技术文章 正文
实现功能:
python文本分析与挖掘(一)-构建语料库
实现代码:
1 | import os |
2 | from warnings import simplefilter |
3 | simplefilter(action='ignore', category=FutureWarning) |
4 | import os.path |
5 | import codecs |
6 | import pandas |
7 | |
8 | #==========词料库构建================= |
9 | def Create_corpus(file): |
10 | filePaths = [] |
11 | fileContents=[] |
12 | for root, dirs, files in os.walk(file): |
13 | print(root) |
14 | print(dirs) |
15 | print(files) |
16 | # os.path.join()方法拼接文件名返回所有文件的路径,并储存在变量filePaths中 |
17 | for name in files: |
18 | filePath=os.path.join(root, name) |
19 | filePaths.append(filePath) |
20 | print(filePaths) |
21 | f = codecs.open(filePath, 'r', 'utf-8') |
22 | print(f) |
23 | fileContent = f.read() |
24 | print(fileContent) |
25 | f.close() |
26 | fileContents.append(fileContent) |
27 | #codecs.open()方法打开每个文件,用文件的read()方法依次读取其中的文本,将所有文本内容依次储存到变量fileContenst中,然后close()方法关闭文件。 |
28 | #创建数据框corpos,添加filePaths和fileContents两个变量作为数组 |
29 | corpos = pandas.DataFrame({'filePath': filePaths,'fileContent': fileContents}) |
30 | print(corpos) |
31 | |
32 | Create_corpus("F:\医学大数据课题\AI_SLE\AI_SLE_TWO\TEST_DATA") |
实现效果:
喜欢记得点赞,在看,收藏,
关注V订阅号:数据杂坛,获取数据集,完整代码和效果,将持续更新!
猜你喜欢
- 2025-07-14 Python数据重塑与清洗常用命令(python数据分析数据清洗)
- 2025-07-14 Python GUI编程利器:Tkinker中的列表框和静态框(5)
- 2025-07-14 Python办公自动化系列课程2:Excel大体联数据匹配只需12行代码
- 2025-07-14 Python数据可视化Dash开源库Bootstrap信息提示框Alert
- 2025-07-14 python 数据处理库中库,增强pandas功能的库有什么特别之处
- 2025-07-14 学习编程第177天 python编程 富文本框text控件的使用
- 07-14Python数据重塑与清洗常用命令(python数据分析数据清洗)
- 07-14python文本分析与挖掘(一)-构建语料库
- 07-14Python GUI编程利器:Tkinker中的列表框和静态框(5)
- 07-14Python办公自动化系列课程2:Excel大体联数据匹配只需12行代码
- 07-14Python数据可视化Dash开源库Bootstrap信息提示框Alert
- 07-14python 数据处理库中库,增强pandas功能的库有什么特别之处
- 07-14学习编程第177天 python编程 富文本框text控件的使用
- 07-14从逗号到省略号,Python 标点符号用法大揭秘!看完少踩 90% 的坑!
- 277℃Python短文,Python中的嵌套条件语句(六)
- 277℃python笔记:for循环嵌套。end=""的作用,图形打印
- 274℃PythonNet:实现Python与.Net代码相互调用!
- 269℃Python实现字符串小写转大写并写入文件
- 268℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 127℃原来2025是完美的平方年,一起探索六种平方的算吧
- 113℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 108℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 最近发表
-
- Python数据重塑与清洗常用命令(python数据分析数据清洗)
- python文本分析与挖掘(一)-构建语料库
- Python GUI编程利器:Tkinker中的列表框和静态框(5)
- Python办公自动化系列课程2:Excel大体联数据匹配只需12行代码
- Python数据可视化Dash开源库Bootstrap信息提示框Alert
- python 数据处理库中库,增强pandas功能的库有什么特别之处
- 学习编程第177天 python编程 富文本框text控件的使用
- 从逗号到省略号,Python 标点符号用法大揭秘!看完少踩 90% 的坑!
- 一篇文章带你弄懂Python基础之列表介绍和循环遍历
- Python NumPy 数组形状操作(python数组类型)
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)