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

使用python对具有异常分隔符的文本文件进行解析,python,parsing,text,Python,Parsing,Text,在支持遗留系统时,我面临一个字段数据采集器,它以以下格式存储数据: # 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

在支持遗留系统时,我面临一个字段数据采集器,它以以下格式存储数据:

# 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

#这是一条评论我将分三步进行:

  • 删除文件开头的所有注释
  • 在正则表达式上拆分以查找文件中的所有其他注释(有关如何使用正则表达式拆分的示例,请参见)
  • 解析其余的行

  • 您可以使用正则表达式并执行以下操作:
    ^(?=\\\\\\?\d+\)

    此处举例说明: