2013-03-11 74 views
1

在支持传统系统不同寻常的分隔符的文本文件,我面对的是存储在以下格式数据的字段数据收集:解析了有使用Python

# 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

我会做这三个步骤:

  1. 从正则表达式的文件
  2. 分裂的开始删除所有注释来查找文件中的所有其他意见(见here为例如如何使用正则表达式)
  3. 解析其余线路