之前为了大家可以将收集的数据便于分析,建议大家使用csv这种表样式数据格式进行保存。但是咱们爬数据时,除了一些数值类,大部分数据是文本,如何操作csv每一行中的文本数据呢? 例如我有 局座哭了.csv,我们如何对comment列进行数据清理,只保留中文。 操作数据时,如何保持原来的数据结构(比如分词处理后,csv文件除了加了一列新的分词记录外,原来的数据是否保留,能否一一对应)? 思路: 1、用pandas库读入csv文件为dataframe 2、将dataframe转化为python对象 3、写一个处理函数(如数据操作函数,如分词) 4、在此调用处理函数,得到新数据 5、将原csv数据和新数据写入新csv 一、读入csv文件首先要将csv文件查看下,整理成utf-8编码形式 然后使用pandas库的read_csv()方法,直接看案例学习吧 import pandas as pd
path = r'C:\Users\thunderhit\Desktop\局座哭了.csv'
f = open(path,'r',encoding='utf-8') #mac不用这行,直接pd.read_csv(path)
data = pd.read_csv(f)
data #了解数据的概况,如count为记录数(即131行)
data.describe()
二、将dataframe转化为python对象我们看到 局座哭了.csv 有131行数据。 能不能循环的方式,把每行遍历出来? 能不能再对每行的每个元素进行抽取,比如抽取出每行中的comment 2.1 遍历dataframe中的每一行首先我们要有知道行数,data.describe()告诉我们有131行 那任意的csv文件有多少行(record_num行数),我们必须自动化的知道 record_num = df.describe().ix[0,0] record_num = int(data.describe().ix[0,0])
record_num 131 2.2 遍历每行出来比如我要看第一行的所有列的数据 data.ix[0,:] username SIDNEY1987
verified False
verified_type -1
profile_url http://m.weibo.cn/u/2269153534?uid=2269153534&...
source 微博 weibo.com
review_id 4.10082e+15
like_counts 0
image https://tva4.sinaimg.cn/crop.0.77.310.310.180/...
date 1分钟前
comment 这是家里吗
Name: 0, dtype: object #遍历出所有行
for i in range(record_num):
record = data.ix[i,:]
print(record) username SIDNEY1987
verified False
verified_type -1
profile_url http://m.weibo.cn/u/2269153534?uid=2269153534&...
source 微博 weibo.com
review_id 4.10082e+15
like_counts 0
image https://tva4.sinaimg.cn/crop.0.77.310.310.180/...
date 1分钟前
comment 这是家里吗
Name: 0, dtype: object
username Chow丶won_Chan
verified False
verified_type -1
profile_url http://m.weibo.cn/u/1956942095?uid=1956942095&...
source iPhone 6 Plus
review_id 4.10082e+15
like_counts 0
image https://tva4.sinaimg.cn/crop.0.2.1242.1242.180...
date 1分钟前
comment 胡建人发来贺定
Name: 1, dtype: object 2.3 对每一行中的某列进行操作比如提取出第1行中的comment record1 = data.ix[0,:]
record1['comment'] '这是家里吗' #遍历出所有行的comment
for i in range(record_num):
record = data.ix[i,:]
comment = record['comment']
print(comment) 这是家里吗
胡建人发来贺定
恩,这是我们的国家――中国
河南南阳人民发来贺电
<span class="url-icon"><img src="//h5.sinaimg.cn/m/emoticon/icon/others/h_good-55854d01bb.png" style="width:1em;height:1em;;"></span><span class="url-icon"><img src="//h5.sinaimg.cn/m/emoticon/icon/others/h_good-55854d01bb.png" style="width:1em;height:1em;;"></span><span class="url-icon"><img src="//h5.sinaimg.cn/m/emoticon/icon/others/h_good-55854d01bb.png" style="width:1em;height:1em;;"></span>
早啊
回复<a href='http://m.weibo.cn/n/半鬓残香'>@半鬓残香</a>:河南财经政法大学发来贺电
重庆人民发来贺电!
<span class="url-icon"><img src="//h5.sinaimg.cn/m/emoticon/icon/default/d_guzhang-4ac2b70b8f.png" style="width:1em;height:1em;;"></span><span class="url-icon"><img src="//h5.sinaimg.cn/m/emoticon/icon/default/d_guzhang-4ac2b70b8f.png" style="width:1em;height:1em;;"></span><span class="url-icon"><img src="//h5.sinaimg.cn/m/emoticon/icon/default/d_guzhang-4ac2b70b8f.png" style="width:1em;height:1em;;"></span><span class="url-icon"><img src="//h5.sinaimg.cn/m/emoticon/icon/default/d_guzhang-4ac2b70b8f.png" style="width:1em;height:1em;;"></span>
<span class="url-icon"><img src="//h5.sinaimg.cn/m/emoticon/icon/others/f_v5-d9ecc60931.png" style="width:1em;height:1em;;"></span><span class="url-icon"><img src="//h5.sinaimg.cn/m/emoticon/icon/others/f_v5-d9ecc60931.png" style="width:1em;height:1em;;"></span>
comment
comment
comment
回复<a href='https://m.weibo.cn/n/chao-hui'>@chao-hui</a>:但是你看看评论,远超所谓的热搜
回复<a href='https://m.weibo.cn/n/卡秃噜皮儿'>@卡秃噜皮儿</a>:老乡…😂
莫名其妙的想哭 三、写一个处理函数如数据操作函数,如只保留中文数据 import re
def Chinese(text):
cleaned = re.findall(r'[\u4e00-\u9fa5]+', text) #返回列表
cleaned = ''.join(cleaned) #拼接成字符串
return cleaned
commentt = '>@半鬓残香</a>:河南信阳发来贺电<span class="url-icon">'
Chinese(commentt) '半鬓残香河南信阳发来贺电' 四、在此调用处理函数,得到新数据#在循环中调用函数,对每行的comment操作,只保留中文字符
for i in range(record_num):
record = data.ix[i,:]
comment = record['comment']
cleaned = Chinese(comment)
print(cleaned) 这是家里吗
胡建人发来贺定
恩这是我们的国家中国
河南南阳人民发来贺电
早啊
回复半鬓残香半鬓残香河南财经政法大学发来贺电
重庆人民发来贺电
回复但是你看看评论远超所谓的热搜
回复卡秃噜皮儿卡秃噜皮儿老乡
莫名其妙的想哭 五、将原csv数据和新数据写入新csvimport csv
#建新csv文件
path = r'C:\Users\thunderhit\Desktop\新csv文件.csv'
csvfile = open(path,'w',encoding='utf-8')
writer = csv.writer(csvfile)
writer.writerow(('username','verified','verified_type','profile_url','source','review_id','like_counts','image','date','comment'))
for i in range(record_num):
record = data.ix[i,:]
comment = record['comment']
comment = Chinese(comment)
writer.writerow((record['username'],record['verified'],record['verified_type'],record['profile_url'],
record['source'],record['review_id'],record['like_counts'],record['image'],record['date'],comment))
csvfile.close() 检查一下我们检查下新的csv文件,是否如我们意 comment列是否已经清理,只保留中文 path = r'C:\Users\thunderhit\Desktop\新csv文件.csv'
f = open(path,'r',encoding='utf-8') #mac不用这行,直接pd.read_csv(path)
pd.read_csv(f)
更多内容 文本分析 数据分析 神奇的python 爬虫 【视频】有了selenium,小白也可以自豪的说:“去TMD的抓包、cookie” |
|