2016-09-12 2 views
2

Ich habe eine Reihe von Nachrichtenartikeln im JSON-Format und ich habe Probleme beim Parsing des Datums der Daten. Das Problem ist, dass sobald die Artikel im JSON-Format konvertiert wurden, das Datum erfolgreich konvertiert wurde, aber auch die Ausgabe. Hier ist eine Abbildung:Parsing-Datum im JSON-Format mit Python

{"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.;"} 

Da ich bei Zählung der Anzahl der Artikel bin mit dem Ziel, die bestimmte Worte, die ich Schleife enthalten die Artikel in der folgenden Art und Weise:

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) 

ich suche nach der Orientierung, wie man das Problem auf die effizienteste Weise angehen. Ich dachte an etwas wie "ignoriere alles, was nach dem Datum der Woche geht", wenn ich das Datum analysiere. Gibt es so etwas?

Vielen Dank im Voraus

Antwort

2

Sie Spalte date von str.split, concanecate erste und zweite Säule aufspalten - month, day und year zusammen (December 31 und 1995) und letzter Aufruf 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.; 
+0

funktioniert super Kumpel , vielen Dank! –

+1

Froh kann dir helfen! – jezrael

+0

Hi @jezrael, ich habe eine Situation gefunden, in der das Jahr am Ende kein Komma hat: "31. Dezember 1995" statt "31. Dezember 1995". Die Analyse in diesem Szenario scheint zu hängen. Irgendwelche Ideen, wie man es löst, vielen Dank! –