【问题标题】:Convert dictionary format txt file into excel in Python在Python中将字典格式txt文件转换为excel
【发布时间】:2019-10-28 18:22:49
【问题描述】:

对于一个txt文件result.txt如下:

[
  {
    "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
  }
]

我想将其转换为数据框 df 然后另存为 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

如何在 Python 中做到这一点?谢谢。

首先我用 Python 读取文件:

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

print(data)

输出:

['[\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']

【问题讨论】:

    标签: python pandas dataframe dictionary


    【解决方案1】:

    用途:

    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
    

    【讨论】:

      【解决方案2】:

      你的文本文件是 json 格式,所以如果没有扩展名.json,你也可以使用read_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
      

      DataFrame.to_excel 上次写入 excel:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-28
        • 1970-01-01
        • 2021-08-17
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 2015-07-13
        相关资源
        最近更新 更多