python数据处理详情
作者:上进小菜猪 时间:2021-05-08 12:53:12
一,前言
我们现在拿到了一个十分庞大的数据集。是json
文件,里面存储了将近十万个数据,现在要对其中的数据进行清洗处理。
二,python模块
import json
import jieba
我们需要用json模块来处理json文件,和使用jieba库来分析词性,这样可以实现我们的需求。
2.1,增加停用词表
停用词表.txt,把停用词表存入stopwords
,原因是:我们的目标分析json里有一些标点符号。
stopwords = [line.strip() for line in open("停用词表.txt",encoding="utf-8").readlines()]
基本如图所示:
a+str(b)+c
这是文件名称,a+b+c=./json/poet.song.0.json b
递增,实现动态取值
with open(a+str(b)+c,'r',encoding='utf8')as fp:
因为有将近500个json文件。每个文件里有好几千组数据,我现在尽力的优化代码,现在提取一次,把需要的数据存入文件里面差不多需要五分钟。
2.2,顺序读取
定义一个空的字符串,将json对象转换为python对象。定义一个空的list存放诗句。
循环
json_data i
为里面的每一个元素。新的追加到
list_paragraphs
列表循环 j为里面的每一句。
代码如图所示:
使用jieba库,分析str内容的词性【注意是名称,动词。。。。】排行输出都是俩个字是巧合,没有字数限制
words = jieba.lcut(str_s)
现在words为分析完毕的词性列表,遍历。
排除特殊符号
for word in words:
if word not in stopwords:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word,0) + 1
出现频率加一。
2.3,lambda函数
使用lambda函数,sort快速排序,遍历输出频率前50的词性。
items.sort(key=lambda x:x[1], reverse=True)
之后赋值word
, count
。
word, count = items[i]
print ("{:<10}{:>7}".format(word, count))
三,运行
3.1,存入文件
f=open('towa.txt',"a",encoding='gb18030')
f.writelines("题目:"+textxxx)
f.writelines(word_ping)
来源:https://blog.csdn.net/weixin_52908342/article/details/124065858
标签:python,数据,处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解用Python实现自动化监控远程服务器
2021-02-14 15:50:57
![](https://img.aspxhome.com/file/2023/9/109629_0s.png)
python Tkinter的图片刷新实例
2023-10-31 04:32:24
oracle命令行删除与创建用户的代码
2009-03-02 10:54:00
pandas检查和填充缺失值的N种方法总结
2021-03-27 03:25:21
![](https://img.aspxhome.com/file/2023/6/80806_0s.png)
Python使用Keras OCR实现从图像中删除文本
2022-07-22 20:50:24
![](https://img.aspxhome.com/file/2023/9/80569_0s.jpg)
Pycharm以root权限运行脚本的方法
2022-03-16 12:23:51
![](https://img.aspxhome.com/file/2023/0/107110_0s.jpg)
Pandas中DataFrame交换列顺序的方法实现
2023-01-21 10:21:08
详解利用django中间件django.middleware.csrf.CsrfViewMiddleware防止csrf攻击
2023-03-16 14:33:51
实例讲解Python中的私有属性
2023-11-10 16:41:46
python关闭占用端口方式
2022-03-26 14:10:53
做网站要习惯化与去习惯化
2008-06-18 13:36:00
在ASP中使用SQL语句之1:SELECT 语句
2007-08-11 12:18:00
javascript 的 in 操作符实例详解
2007-10-07 12:00:00
python进阶_浅谈面向对象进阶
2022-01-22 17:02:06
Python正规则表达式学习指南
2021-04-11 15:21:16
![](https://img.aspxhome.com/file/2023/7/68827_0s.png)
python通过pip更新所有已安装的包实现方法
2021-06-04 03:22:34
利用Python如何生成hash值示例详解
2022-02-28 23:29:25
Python用SSH连接到网络设备
2022-01-20 08:57:41
![](https://img.aspxhome.com/file/2023/1/89191_0s.png)
使用Python实现毫秒级抢单功能
2022-10-17 18:46:16
![](https://img.aspxhome.com/file/2023/3/109583_0s.png)
PHPExcel冻结(锁定)表头的简单实现方法
2023-08-18 02:35:21
![](https://img.aspxhome.com/file/2023/9/55379_0s.jpg)