【问题标题】:How to extract specific parts from specific lines from a file?如何从文件的特定行中提取特定部分?
【发布时间】:2020-07-15 18:06:34
【问题描述】:

我有一个 .yml 文件,我需要从特定行中提取特定部分。

这是文件的一部分(文件有 1200 多行,但结构始终相似):

training:
trainings:
  - workout: Rec 016
    performed_at: 2020-06-25 09:04:16.295000076 Z
    star: false
    time: '00:04:00'
  - workout: Hanging knee raises endurance 10
    performed_at: 2020-06-25 08:59:11.871999979 Z
    star: true
    time: '00:00:28'
    repetitions: 10
  - workout: Str 700
    performed_at: 2020-06-25 08:57:51.039999961 Z
    star: true
    time: '00:15:30'
  - workout: Supermans technical 30
    performed_at: 2020-06-25 08:38:45.894000053 Z
    star: true
    time: '00:01:02'
  - workout: Toe touch crunches technical 20
    performed_at: 2020-06-25 08:37:05.439000129 Z
    star: true
    time: '00:00:54'
  - workout: Pre 028
    performed_at: 2020-06-25 08:35:33.243999958 Z
    star: false
    time: '00:06:30'
  - workout: Rec 001
    performed_at: 2020-06-22 22:51:38.947000026 Z
    star: false
    time: '00:05:01'
  - workout: Burpees standard 10
    performed_at: 2020-06-22 22:46:00.807000160 Z
    star: true
    time: '00:00:38'

额外信息:使用以下代码:

df = pd.read_csv(r'text_data.yml')

with pd.option_context("display.max_rows", None, "display.max_columns", None):
    print(df)

这是文件转换成的内容:

                                                    ---
0                                             training:
1                                            trainings:
2                                    - workout: Rec 016
3         performed_at: 2020-06-25 09:04:16.295000076 Z
4                                           star: false
5                                      time: '00:04:00'
6           - workout: Hanging knee raises endurance 10
7         performed_at: 2020-06-25 08:59:11.871999979 Z
8                                            star: true
9                                      time: '00:00:28'
10                                      repetitions: 10
11                                   - workout: Str 700
12        performed_at: 2020-06-25 08:57:51.039999961 Z
13                                           star: true
14                                     time: '00:15:30'
15                    - workout: Supermans technical 30
16        performed_at: 2020-06-25 08:38:45.894000053 Z
17                                           star: true
18                                     time: '00:01:02'
19           - workout: Toe touch crunches technical 20
20        performed_at: 2020-06-25 08:37:05.439000129 Z
21                                           star: true
22                                     time: '00:00:54'
23                                   - workout: Pre 028
24        performed_at: 2020-06-25 08:35:33.243999958 Z
25                                          star: false
26                                     time: '00:06:30'
27                                   - workout: Rec 001
28        performed_at: 2020-06-22 22:51:38.947000026 Z
29                                          star: false
30                                     time: '00:05:01'

我要做的是提取以“performed_at:”开头的行中的日期(仅日期,没有其他),并将它们放入列表/数据帧中。

我将如何通过 Pandas 以最有效的方式执行此操作?

【问题讨论】:

  • 这能回答你的问题吗? How to denormalize YAML for Pandas Dataframe?
  • YML 文件是一个 YAML 文件。但是,我看到我链接的答案中没有提到它,但答案使用了外部 pyyaml 库(yaml.load(f) 是这个库的一部分)。你能描述一下它到底是怎么不工作的吗?
  • 还要检查这个答案,因为它填写得更多:stackoverflow.com/a/53892729/13590089
  • 这似乎对我有用。做最后一件事,为我试试这个:with open('file.yml', 'r') as f: df = pd.io.json.json_normalize(yaml.load(f), 'trainings') 这给了我一个包含所有字段的数据框,然后我可以从中选择一个 perform_at 列。这直接取自之前发布的问题中的答案(因此我在这里发布而不是回答)
  • 很有可能。我希望有人可能有其他建议。但是,以防万一,如果您的数据的 'trainings' 部分在 .yml 文件中缩进,您必须将 'trainings'(在 json_normalize 调用中)替换为 'trainings' 和所有前面的标头的列表,例如,如果整个 'listings' 部分比 'training' 缩进了一个额外的步骤,则您必须传递 ['training', 'trainings'] 而不仅仅是 'trainings'

标签: python pandas list file yaml


【解决方案1】:

有关解决方案,请阅读问题下的 cmets。感谢@dm2。

【讨论】:

    猜你喜欢
    • 2019-07-12
    • 1970-01-01
    • 2022-10-24
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多