我有这段代码,它将用户选择的日志文件转换为 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/