简体   繁体   中英

Convert dictionary format txt file into excel in Python

For a txt file result.txt as follows:

[
  {
    "image_id": "42041",
    "mean_score_prediction": 4.996936075389385
  },
  {
    "image_id": "42039",
    "mean_score_prediction": 4.647608995437622
  },
  {
    "image_id": "42044",
    "mean_score_prediction": 3.9866196922957897
  },
  {
    "image_id": "42042",
    "mean_score_prediction": 3.9691513180732727
  },
  {
    "image_id": "42040",
    "mean_score_prediction": 4.303698152303696
  }
]

I want to convert it to a dataframe df then save as excel result.xlsx :

print(df)
   image_id  mean_score_prediction
0     42041               4.996936
1     42039               4.647609
2     42044               3.986620
3     42042               3.969151
4     42040               4.303698

How can I do this in Python? Thanks.

First I read the file in Python:

filename = 'result.txt'
with open(filename) as f:
    data = f.readlines()

print(data)

Output:

['[\n', '  {\n', '    "image_id": "42041",\n', '    "mean_score_prediction": 4.996936075389385\n', '  },\n', '  {\n', '    "image_id": "42039",\n', '    "mean_score_prediction": 4.647608995437622\n', '  },\n', '  {\n', '    "image_id": "42044",\n', '    "mean_score_prediction": 3.9866196922957897\n', '  },\n', '  {\n', '    "image_id": "42042",\n', '    "mean_score_prediction": 3.9691513180732727\n', '  },\n', '  {\n', '    "image_id": "42040",\n', '    "mean_score_prediction": 4.303698152303696\n', '  }\n', ']\n']

Use:

In [1]: import pandas as pd

In [2]: with open("result.txt", 'r') as f:
   ...:     data = f.read()
   ...:

In [3]: data
Out[3]: '[\n  {\n    "image_id": "42041",\n    "mean_score_prediction": 4.996936075389385\n  },\n  {\n    "image_id": "42039",\n    "mean_score_prediction": 4.647608995437622\n  },\n  {\n    "image_id": "42044",\n    "mean_score_prediction": 3.9866196922957897\n  },\n  {\n    "image_id": "42042",\n    "mean_score_prediction": 3.9691513180732727\n  },\n  {\n    "image_id": "42040",\n    "mean_score_prediction": 4.303698152303696\n  }\n]'

In [6]: df = pd.read_json(data)

In [7]: df
Out[7]:
   image_id  mean_score_prediction
0     42041               4.996936
1     42039               4.647609
2     42044               3.986620
3     42042               3.969151
4     42040               4.303698

Your text file has json format, so you can use read_json also if no extension .json :

df = pd.read_json('result.txt')
print (df)
   image_id  mean_score_prediction
0     42041               4.996936
1     42039               4.647609
2     42044               3.986620
3     42042               3.969151
4     42040               4.303698

Last write to excel by DataFrame.to_excel :

df.to_excel('result.xlsx', index=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM