【问题标题】:Conversion from YAML to XLSX using Python使用 Python 从 YAML 转换为 XLSX
【发布时间】:2021-07-13 11:02:25
【问题描述】:

我想将文件夹中的一组 yaml 文件转换为 xlsx 文件。我想我会先尝试将一个 yaml 文件转换为一个 xlsx 文件。文件夹中的yaml文件格式如下:

info:
    city: Bangalore
    competition: IPL
    dates:
       - 2008-04-18
    gender: male
    match_type: T20
    outcome:
           by:
              runs: 140
           winner: Kolkata Knight Riders
    overs: 20
    player_of_match:
        - BB McCullum
    teams:
        - Royal Challengers Bangalore
        - Kolkata Knight Riders
    toss:
        decision: field
    winner: Royal Challengers Bangalore
    umpires:
        - Asad Rauf
        - RE Koertzen
    venue: M Chinnaswamy Stadium
    innings:
        - 1st innings:
        team: Kolkata Knight Riders
        deliveries:
                     - 0.1:
                           batsman: SC Ganguly
                           bowler: P Kumar
                           extras:
                                  legbyes: 1
                           non_striker: BB McCullum
                           runs:
                                batsman: 0
                                extras: 1
                           total: 1

比赛的每个球(0.2、0.3、0.4 ... 20.0)的数据继续,并转移到比赛的下半场(第二局)并继续进一步

我尝试将这些 yaml 文件之一转换为 xlsx 文件:

import pandas as pd
import yaml as ya
with open(r"location of folder") as f:
    
    data = ya.load(f, Loader=ya.FullLoader)
    df1=pd.DataFrame(data['info'])
    df1.to_excel(r"location of folder\output.xlsx")

但是,运行上面的代码后,我得到了以下错误:

File "c:\Users\kosal\hello\prj.py", line 8, in <module>
    df1=pd.DataFrame(data['info'])
  File "C:\Users\kosal\anaconda3\lib\site-packages\pandas\core\frame.py", line 529, in __init__
    mgr = init_dict(data, index, columns, dtype=dtype)
  File "C:\Users\kosal\anaconda3\lib\site-packages\pandas\core\internals\construction.py", line 287, in init_dict
    return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
  File "C:\Users\kosal\anaconda3\lib\site-packages\pandas\core\internals\construction.py", line 80, in arrays_to_mgr
    index = extract_index(arrays)
  File "C:\Users\kosal\anaconda3\lib\site-packages\pandas\core\internals\construction.py", line 401, in extract_index
    raise ValueError("arrays must all be same length")

我确实知道为什么会出现这个错误,但我不知道应该如何修复它。

附:我找不到适合这个问题的标签,因此使用了“python”标签。

【问题讨论】:

    标签: python-3.x pandas yaml


    【解决方案1】:

    你遇到过yaml格式的困惑,首先你应该检查你的yaml格式,注意每个缩进

    您的情况下的 Yaml 格式示例

    info:
      city: Bangalore
      competition: IPL
      dates:
        - 2008-04-18
      gender: male
      match_type: T20
      outcome:
        by:
          runs: 140
          winner: Kolkata Knight Riders
      overs: 20
      player_of_match:
        - BB McCullum
      teams:
        - Royal Challengers Bangalore
        - Kolkata Knight Riders
      toss:
        decision: field
      winner: Royal Challengers Bangalore
      umpires:
        - Asad Rauf
        - RE Koertzen
      venue: M Chinnaswamy Stadium
      innings:
        - 1st_innings:
          team: Kolkata Knight Riders
          deliveries:
            - 0.1:
              batsman: SC Ganguly
              bowler: P Kumar
              extras:
                legbyes: 1
              non_striker: BB McCullum
              runs:
                batsman: 0
                extras: 1
              total: 1
    

    然后你检查你的

    pip install pyyaml
    pip install pandas
    pip install openpyxl
    
    import pandas as pd
    import yaml as ya
    with open(r"location of folder") as f:
        
        data = ya.load(f, Loader=ya.FullLoader)
        df1=pd.DataFrame(data['info'])
        df1.to_excel(r"location of folder\output.xlsx")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 2018-08-27
      • 2019-02-18
      • 2013-05-27
      • 2023-01-11
      • 2018-03-22
      • 2016-02-16
      相关资源
      最近更新 更多