使用Python清理文本和停止词后,将数据从Json导入到Excel

2024-09-29 03:29:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Json文件,其中包含了我使用Scrapy从一个网站抓取的数据,我的下一步是清除数据文本中的特殊字符和停止字,并保存在Excel文件中,以备下一步。 数据如下:

{"title": ["\u2605\u2605 The New J7 - Social Offer \u2605\u2605"], "seller": ["Galaxy"]}

我需要做的是:

  1. 阅读每个项目

  2. 删除特殊字符,我不知道如何读取,因为它们是这样解码的:\u2605\u2605

  3. 删除停止语

  4. 将新数据保存到Excel文件中

我读过很多关于将Json导入Excel的线程,但是都声明了一种在整个块中同时导入Json而不修改数据的方法。在

编辑:

这是我最后的代码,它读取json文件,编辑值并保存到excel中,我希望它也能帮助其他人。在

^{pr2}$

Tags: 文件the数据文本json编辑newtitle
1条回答
网友
1楼 · 发布于 2024-09-29 03:29:06

在这种情况下,熊猫是你的朋友。在

import pandas as pd

df = pd.read_json('{"title": ["\u2605\u2605 The New J7 - Social Offer \u2605\u2605"], "seller": ["Galaxy"]}')
# Remove unneeded special characters by encoding to ascii and then recoding to utf-8
df.title = df.title.str.encode('ascii', 'ignore')
df.title = df.title.str.decode('utf-8')

# Removing stopwords - you need a list called stopwords defining your stopwords
df.title = df.title.apply(lambda x: ' '.join([word for word in x.split() if word not in (stopwords)]))

# write to excel
df.to_excel('out.xlsx')

要获得一个停止字列表,如果您还没有,您应该调查nltk。在

相关问题 更多 >