python - 将log转成json存入内存

标签 python json python-3.x pandas logging

我有这段代码,它将用户选择的日志文件转换为 json 并将其存储到文件中(使用 pandas 的 to_json 函数)。

import pandas as pd

with open(self.get_logfile_path()) as log_file:
     log_file = log_file.read().splitlines()


df = pd.DataFrame(log_file)

df.to_json(r'C:\Users\xx\Downloads\abc.json')

我的问题是,如何将转换后的 json 文件临时存储到内存中(每个键都是换行符的开头),而不是将其输出到某个目录中?我使用的是 Python 3.8.1。

最佳答案

试试这个:

import pandas as pd
import json

# Convert dataframe "df" to dictionary
df_dict = df.to_dict()

#Store the dictionary into a json string variable in memory
df_json = json.dumps(df_dict)

#Print the json contents
print(df_json)

关于python - 将log转成json存入内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60279039/

相关文章:

python - 启动客户端时,代码要求电话/机器人 token

mysql - Node.js 用 mysql 编写 api 代码(sequelize)

java - 带有 DefaultHttpClient 和 POST over SSL 的 Android JSON 解析器

python - python 3.5 中的 dict.popitem()

使用动态 "is in"约束的 Python 字典架构验证

python - 查找Python中所有解决方案列表中是否存在一组潜力的最快方法

python - 将每个表写入 csv,后跟标题

python - 名为断言的函数在模拟导入类上失败

python - 计算未成年人的矩阵行列式!

javascript - '类型错误: undefined is not a function' when pulling json data from the github api in Angular?