我有这段代码片段,它将用户选择的日志文件转换为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)
[如果有任何答案可以帮助您在StackOverflow上进行投票,请不要将其标记并标记为接受。干杯!