2013-03-11 72 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. 解析其餘線路