python - 读取特定 JSON 列以进行标记化
问题描述
我打算用 NLTK 标记 JSON 文件中的列。下面的代码根据不同的时间间隔读取 JSON 文件并将其切片。
但是,我正在努力让'Main Text'
列(在 JSON 文件中)在下面代码的最后部分中读取/标记。是否有任何聪明的调整来实现这一点?
# Loading and reading dataset
file = open("Glassdoor_A.json", "r")
data = json.load(file)
df = pd.json_normalize(data)
df['Date'] = pd.to_datetime(df['Date'])
# Create an empty dictionary
d = dict()
# Filtering by date
start_date = pd.to_datetime('2009-01-01')
end_date = pd.to_datetime('2009-03-31')
last_end_date = pd.to_datetime('2017-12-31')
mnthBeg = pd.offsets.MonthBegin(3)
mnthEnd = pd.offsets.MonthEnd(3)
while end_date <= last_end_date:
filtered_dates = df[df.Date.between(start_date, end_date)]
n = len(filtered_dates.index)
print(f'Date range: {start_date.strftime("%Y-%m-%d")} - {end_date.strftime("%Y-%m-%d")}, {n} rows.')
if n > 0:
print(filtered_dates)
start_date += mnthBeg
end_date += mnthEnd
# NLTK tokenizing
file_content = open('Main Text').read()
tokens = nltk.word_tokenize(file_content)
print(tokens)
解决方案
我已经用下面的代码解决了这个问题,运行顺利。再次感谢大家的意见。
for index, row in filtered_dates.iterrows():
line = row['Text Main']
tokens = nltk.word_tokenize(line)
print(tokens)
推荐阅读
- python - 在运行时在 Lambda 中加载 Python 包
- java - 将列表中最喜欢的项目存储到android中的SharedPreferences中?
- security - 出于安全原因,请删除拉取请求的整个记录,或至少部分历史记录
- python - 我可以在本地 Wamp 服务器上使用 OpenGL 吗?
- java - 使用 java 加密库和 RSA 4096 PublicKey 的加密无法正常工作
- android - NPM 缓存步骤在 Azure DevOps 中不起作用
- arrays - 连接两个递增子数组的最大长度递增超级数组
- elasticsearch - simple_query_string 的弹性搜索问题
- typescript - Typescript 枚举作为参数类型允许无效值
- javascript - 根据 url 设置 src