日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python數據處理詳情_python

作者:上進小菜豬 ? 更新時間: 2022-06-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

欄目分類
最近更新