gpt4 book ai didi

python - 使用 Python 解析 JSON 格式的日期

转载 作者:太空宇宙 更新时间:2023-11-04 03:06:00 25 4
gpt4 key购买 nike

我有一组 JSON 格式的新闻文章,但在解析数据日期时遇到问题。问题是,一旦文章被转换成 JSON 格式,日期就成功转换了,但也做了版本。这是一个例子:

{"date": "December 31, 1995, Sunday, Late Edition - Final", "body": "AFTER a year of dizzying new heights for the market, investors may despair of finding any good stocks left. Navistar plans to slash costs by $112 million in 1996. Advanced Micro Devices has made a key acquisition. For the bottom-fishing investor, therefore, the big nail-biter is: Will the changes be enough to turn a company around? ", "title": "INVESTING IT;"}
{"date": "December 31, 1995, Sunday, Late Edition - Final", "body": "Few issues stir as much passion in so many communities as the simple act of moving from place to place: from home to work to the mall and home again. It was an extremely busy and productive year for us, said Frank J. Wilson, the State Commissioner of Transportation. There's a sense of urgency to get things done. ", "title": "ROAD AND RAIL;"}
{"date": "December 31, 1996, Sunday, Late Edition - Final", "body": "Widespread confidence in the state's economy prevailed last January as many businesses celebrated their most robust gains since the recession. And Steven Wynn, the chairman of Mirage Resorts, who left Atlantic City eight years ago because of local and state regulations, is returning to build a $1 billion two-casino complex. ", "title": "NEW JERSEY & CO.;"}

因为我的目标是计算包含特定单词的文章数量,所以我按以下方式循环文章:

import json
import re
import pandas

for i in range(1995,2017):
df = pandas.DataFrame([json.loads(l) for l in open('USAT_%d.json' % i)])
# Parse dates and set index
df.date = pandas.to_datetime(df.date) # is giving me a problem
df.set_index('date', inplace=True)

我正在关注如何以最有效的方式解决问题。在解析日期时,我正在考虑诸如“忽略星期几之后发生的任何事情”之类的事情。有这种东西吗?

提前致谢

最佳答案

您可以按 str.split 拆分列 date , 将第一列和第二列 - monthdayyear 连接在一起(December 311995) 和最后一次通话 to_datetime :

for i in range(1995,2017):
df = pandas.DataFrame([json.loads(l) for l in open('USAT_%d.json' % i)])
# Parse dates and set index
#print (df)
a = df.date.str.split(', ', expand=True)
df.date = a.iloc[:,0] + ' ' + a.iloc[:,1]
df.date = pandas.to_datetime(df.date)
df.set_index('date', inplace=True)
print (df)

body \
date
1995-12-31 AFTER a year of dizzying new heights for the m...
1995-12-31 Few issues stir as much passion in so many com...
1996-12-31 Widespread confidence in the state's economy p...

title
date
1995-12-31 INVESTING IT;
1995-12-31 ROAD AND RAIL;
1996-12-31 NEW JERSEY & CO.;

关于python - 使用 Python 解析 JSON 格式的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39448713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com