【问题标题】:Pandas dataframe wrong number of rows熊猫数据框错误的行数
【发布时间】:2016-07-13 01:12:03
【问题描述】:

我有一个相当大的 json 日志数据文件,我正在尝试将其转换为 XLS 或 CSV。 这个过程中的某些东西只占用了前 1000 行,我不知道是什么原因造成的。

import json
import pprint
import pandas as pd
from pandas.io.json import json_normalize

f = open('GetLog.json', 'r')
writer = pd.ExcelWriter('output.xlsx')
payload = json.load(f)
df = json_normalize(payload, 'Result')
f.close()

pprint.pprint(df)
df.to_excel(writer,'Log Output')
writer.save()
writer.close()

略过净化的 json 提取如下,但足以说明我只对结果感兴趣,因为消息的有效负载通常是空的。

{"Log":{"Messages":[]},"Result":[{"logdate":"/Date(1468270785461)/","message":"ErrorText","logtype":0, “模块”:“WatchFolder”,“logdateStr”:“2016/07/12 06:59:45.461"},{"logdate":"/Date(1468270785430)/","message":"ErrorText","logtype":0,"module":"WatchFolder","logdateStr":"2016 /07/12 06:59:45.430"},{"logdate":"/Date(1468270785398)/","message":"ErrorText","logtype":0,"module":"WatchFolder","logdateStr":"2016 /07/12 06:59:45.398"},{"logdate":"/Date(1468270785367)/","message":"ErrorText","logtype":0,"module":"WatchFolder","logdateStr":"2016 /07/12 06:59:45.367"},{"logdate":"/Date(1468270785336)/","message":"ErrorText","logtype":0,"module":"WatchFolder","logdateStr":"2016 /07/12 06:59:45.336"},{"logdate":"/Date(1468270785227)/","message":"ErrorText","logtype":0,"module":"WatchFolder","logdateStr":"2016 /07/12 06:59:45.227"},{"logdate":"/Date(1468270785196)/","message":"ErrorText","logtype":0,"module":"WatchFolder","logdateStr":"2016 /07/12 06:59:45.196"},{"logdate":"/Date(1468270785164)/","message":"ErrorText","logtype":0,"module":"WatchFolder","logdateStr":"2016 /07/12 06:59:45.164"}],"成功":true,"TotalCount":5648}

尝试直接本地导入 pandas 失败并出现错误:'ValueError: Mixing dicts with non-Series may lead to ambiguous ordering.'

最终,这是一个脚本,我只想指向远程系统上的 Web 服务并每天提取一到两次一小时的日志

【问题讨论】:

    标签: python json csv pandas


    【解决方案1】:

    解决了 - 最终使用 ijson 来加载 json 文件,并且只加载我想要的结果值。 示例代码在这里:

    import csv
    import ijson
    import pprint
    import pandas as pd
    
    from pandas.io.json import json_normalize
    
    #print flattenjson(x)
    #pprint.pprint
    f = open('GetLog.json', 'r')
    writer = pd.ExcelWriter('output.xlsx')
    df = pd.DataFrame()
    
    for item in ijson.items(f, 'Result'):
        df1 = pd.DataFrame(item)
        if df.empty:
            df = df1
        else:
            df.append(df1, ignore_index=True)
    f.close()
    
    df.to_excel(writer,'Log Output')
    writer.save()
    writer.close()
    

    实时版本使用一些参数从服务器获取 json 以指定日期范围。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 2017-06-13
      • 1970-01-01
      • 2020-02-10
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 2018-11-19
      相关资源
      最近更新 更多