gpt4 book ai didi

python - 使用 python 解析具有异常分隔符的文本文件

转载 作者:行者123 更新时间:2023-12-01 05:48:19 25 4
gpt4 key购买 nike

在支持遗留系统时,我面临着一个以以下格式存储数据的现场数据收集器:

# This is a comment <-beacuse it starts at the begining of the file
# This is a comment <- see above
# 1. Item one <- not a comment because it starts with 1.
# Description of Item 1 <- not a comment as it is after a line that starts with a number
data point 1
data point 2
data point etc
3 <-- represents number of data points under Item one

# 2. Item two <-- not a comment
# Description of item 2 <-- not a comment
data point 1
data point ..
data point 100
100
#3. Item three <--- not a comment
# Item three description
0

我不确定解析该文件以将每个项目包含为其自己的列表的正确方法是什么。请注意,有时(但并非总是)数据会在两个不同项目之间添加随机空格。

解析此类文件的正确方法是什么?

最佳答案

我将分三步完成:

  1. 删除文件开头的所有注释
  2. 根据正则表达式进行拆分以查找文件中的所有其他注释(有关如何使用正则表达式进行拆分的示例,请参阅 here)
  3. 解析剩余的行

关于python - 使用 python 解析具有异常分隔符的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15345450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com