我有一个基本上是固定宽度文本文件的数据文件。文本中有可变数量的空格和位置。我正在尝试使用python将文件解析为一个列表,并且无法找出相应的正则表达式(当然我也对非正则表达式选项开放)。
Date Run By Level 1 Level 2 Level 3 Level 4 Level 5 Level 6 Level 7 Level 8 Level 9
11-15-2014 12:27:43 AM 1 ** 259.0
11-15-2014 7:47:09 AM 1 ** 98.0
11-15-2014 3:45:07 PM 1 ** 785.0
11-16-2014 12:27:43 AM 1 ** 245.0
11-16-2014 7:51:36 AM 1 ** 96.0
11-16-2014 3:43:12 PM 1 ** 788.0
11-17-2014 12:27:43 AM 1 ** 248.0
11-17-2014 7:51:21 AM 1 ** 104.0
11-17-2014 12:45:57 PM 1 ** 97.0 257.0 793.0
11-17-2014 3:46:33 PM 1 ** 792.0
11-18-2014 12:32:31 AM 1 ** 253.0
11-18-2014 7:50:31 AM 1 ** 104.0
11-18-2014 3:48:43 PM 1 ** 781.0
11-19-2014 12:30:36 AM 1 ** 260.0
11-19-2014 8:40:26 AM 1 ** 102.0
11-19-2014 3:47:45 PM 1 ** 803.0
11-20-2014 12:28:40 AM 1 ** 243.0
11-20-2014 7:53:38 AM 1 ** 107.0
11-20-2014 3:43:55 PM 1 ** 787.0
11-21-2014 1:03:45 AM 0 PS 245.0
11-21-2014 7:52:55 AM 1 ** 101.0
11-21-2014 3:44:09 PM 1 ** 789.0
11-22-2014 12:37:26 AM 1 ** 250.0
11-22-2014 7:49:55 AM 1 ** 103.0
到目前为止,我已尝试过:
for line in f:
line = re.split(r' (?=[A-Z])| (?=[0-9])| ',line)
但是,我甚至没有对齐列。我需要他们排队下游使用。
所需的输出是(抱歉有限的行数,手动解析它是致命的!)。
['Date', '', 'Run', 'By', 'Level 1', 'Level 2', 'Level 3', 'Level 4', 'Level 5', 'Level 6', 'Level 7', 'Level 8', 'Level 9','\r\n']
['\r\n']
['\r\n']
['11-15-2014', '12:27:43', 'AM 1', '**', '', '259.0', '', '', '', '', '', '', '', '\r\n']
['11-15-2014', '7:47:09', 'AM 1', '**', '98.0', '', '', '', '', '', '', '', '', '\r\n']
['11-15-2014', '3:45:07', 'PM 1', '**', '', '', '785.0', '', '', '', '', '', '', '\r\n']
...
...
['11-17-2014', '12:45:57', 'PM 1', '**', '97.0', '257.0', '793.0', '', '', '', '', '', '', '\r\n']
本质上是13个项目,然后是换行符;将日期和时间组合成一个字段就可以了,大多数情况下我需要日期和三个级别来正确排列;级别1,级别2和级别3只有值。值通常是单个级别/行,但偶尔会有三个级别(如图所示)。
答案 0 :(得分:1)
我不能说它在生产环境中有多可靠,但它适用于示例数据。
假设:
txt='''\
Date Run By Level 1 Level 2 Level 3 Level 4 Level 5 Level 6 Level 7 Level 8 Level 9
11-15-2014 12:27:43 AM 1 ** 259.0
11-15-2014 7:47:09 AM 1 ** 98.0
11-15-2014 3:45:07 PM 1 ** 785.0
11-16-2014 12:27:43 AM 1 ** 245.0
11-16-2014 7:51:36 AM 1 ** 96.0
11-16-2014 3:43:12 PM 1 ** 788.0
11-17-2014 12:27:43 AM 1 ** 248.0
11-17-2014 7:51:21 AM 1 ** 104.0
11-17-2014 12:45:57 PM 1 ** 97.0 257.0 793.0
11-17-2014 3:46:33 PM 1 ** 792.0
11-18-2014 12:32:31 AM 1 ** 253.0
11-18-2014 7:50:31 AM 1 ** 104.0
11-18-2014 3:48:43 PM 1 ** 781.0
11-19-2014 12:30:36 AM 1 ** 260.0
11-19-2014 8:40:26 AM 1 ** 102.0
11-19-2014 3:47:45 PM 1 ** 803.0
11-20-2014 12:28:40 AM 1 ** 243.0
11-20-2014 7:53:38 AM 1 ** 107.0
11-20-2014 3:43:55 PM 1 ** 787.0
11-21-2014 1:03:45 AM 0 PS 245.0
11-21-2014 7:52:55 AM 1 ** 101.0
11-21-2014 3:44:09 PM 1 ** 789.0
11-22-2014 12:37:26 AM 1 ** 250.0
11-22-2014 7:49:55 AM 1 ** 103.0 '''
尝试:
import re
data=txt.splitlines()
header=data.pop(0)
for line in data:
m=re.search(r'^([\d\-\s:]+)(AM|PM)\s+(\d)\s+(..)([\s\d\.]+)$', line)
if m:
l=[]
l.append(m.group(1)+m.group(2))
l.append(m.group(3))
l.append(m.group(4))
l.append([e.strip() for e in re.findall(r'(\s{15,16}|\s*\d+\.\d)', m.group(5))])
print l
打印:
['11-15-2014 12:27:43 AM', '1', '**', ['', '259.0', '', '', '', '', '', '']]
['11-15-2014 7:47:09 AM', '1', '**', ['98.0', '', '', '', '', '', '', '']]
['11-15-2014 3:45:07 PM', '1', '**', ['', '', '785.0', '', '', '', '', '']]
['11-16-2014 12:27:43 AM', '1', '**', ['', '245.0', '', '', '', '', '', '']]
['11-16-2014 7:51:36 AM', '1', '**', ['96.0', '', '', '', '', '', '', '']]
['11-16-2014 3:43:12 PM', '1', '**', ['', '', '788.0', '', '', '', '', '']]
['11-17-2014 12:27:43 AM', '1', '**', ['', '248.0', '', '', '', '', '', '']]
['11-17-2014 7:51:21 AM', '1', '**', ['104.0', '', '', '', '', '', '', '']]
['11-17-2014 12:45:57 PM', '1', '**', ['97.0', '257.0', '793.0', '', '', '', '', '']]
['11-17-2014 3:46:33 PM', '1', '**', ['', '', '792.0', '', '', '', '', '']]
['11-18-2014 12:32:31 AM', '1', '**', ['', '253.0', '', '', '', '', '', '']]
['11-18-2014 7:50:31 AM', '1', '**', ['104.0', '', '', '', '', '', '', '']]
['11-18-2014 3:48:43 PM', '1', '**', ['', '', '781.0', '', '', '', '', '']]
['11-19-2014 12:30:36 AM', '1', '**', ['', '260.0', '', '', '', '', '', '']]
['11-19-2014 8:40:26 AM', '1', '**', ['102.0', '', '', '', '', '', '', '']]
['11-19-2014 3:47:45 PM', '1', '**', ['', '', '803.0', '', '', '', '', '']]
['11-20-2014 12:28:40 AM', '1', '**', ['', '243.0', '', '', '', '', '', '']]
['11-20-2014 7:53:38 AM', '1', '**', ['107.0', '', '', '', '', '', '', '']]
['11-20-2014 3:43:55 PM', '1', '**', ['', '', '787.0', '', '', '', '', '']]
['11-21-2014 1:03:45 AM', '0', 'PS', ['', '245.0', '', '', '', '', '', '']]
['11-21-2014 7:52:55 AM', '1', '**', ['101.0', '', '', '', '', '', '', '']]
['11-21-2014 3:44:09 PM', '1', '**', ['', '', '789.0', '', '', '', '', '']]
['11-22-2014 12:37:26 AM', '1', '**', ['', '250.0', '', '', '', '', '', '']]
['11-22-2014 7:49:55 AM', '1', '**', ['103.0', '']]
答案 1 :(得分:1)
似乎唯一具有可变宽度的部分是日期/时间。我建议这样做:
m = re.match(r'(\d+-\d+-\d+ \d+:\d+:\d+) (.{4})(.{6})(.{16})(.{15})(.{15})', line)
if m:
print [x.strip() for x in m.groups()]
输出:
['11-15-2014 12:27:43', 'AM 1', '**', '', '259.0', '']
['11-15-2014 7:47:09', 'AM 1', '**', '98.0', '', '']
['11-15-2014 3:45:07', 'PM 1', '**', '', '', '785.0']
['11-16-2014 12:27:43', 'AM 1', '**', '', '245.0', '']
['11-16-2014 7:51:36', 'AM 1', '**', '96.0', '', '']
['11-16-2014 3:43:12', 'PM 1', '**', '', '', '788.0']
['11-17-2014 12:27:43', 'AM 1', '**', '', '248.0', '']
['11-17-2014 7:51:21', 'AM 1', '**', '104.0', '', '']
['11-17-2014 12:45:57', 'PM 1', '**', '97.0', '257.0', '793.0']
['11-17-2014 3:46:33', 'PM 1', '**', '', '', '792.0']
['11-18-2014 12:32:31', 'AM 1', '**', '', '253.0', '']
['11-18-2014 7:50:31', 'AM 1', '**', '104.0', '', '']
['11-18-2014 3:48:43', 'PM 1', '**', '', '', '781.0']
['11-19-2014 12:30:36', 'AM 1', '**', '', '260.0', '']
['11-19-2014 8:40:26', 'AM 1', '**', '102.0', '', '']
['11-19-2014 3:47:45', 'PM 1', '**', '', '', '803.0']
['11-20-2014 12:28:40', 'AM 1', '**', '', '243.0', '']
['11-20-2014 7:53:38', 'AM 1', '**', '107.0', '', '']
['11-20-2014 3:43:55', 'PM 1', '**', '', '', '787.0']
['11-21-2014 1:03:45', 'AM 0', 'PS', '', '245.0', '']
['11-21-2014 7:52:55', 'AM 1', '**', '101.0', '', '']
['11-21-2014 3:44:09', 'PM 1', '**', '', '', '789.0']
['11-22-2014 12:37:26', 'AM 1', '**', '', '250.0', '']
['11-22-2014 7:49:55', 'AM 1', '**', '103.0', '', '']
(虽然将AM / PM与时间分组更为典型,但我正在尝试按照所需输出的描述)
答案 2 :(得分:0)
这看起来像tsv
格式,或 t ab s 已完成 v 等。尝试在标签上分割线:
for line in f:
print line.split('\t')
如果是这种情况,您可以使用csv
module,设置标签作为分隔符。
headers = None
for line in input_file:
splits = line.split(' ')
if headers:
print zip(headers, [v.strip() for v in splitter.split(line)])
continue
headers = splits
答案 3 :(得分:0)
我不会使用re.split()
,而是使用类似re.findall()
的{{1}}。
我认识到这很脏,但由于这似乎不是固定长度的空格作为分隔符,这可能会有所帮助。如果数字变大,它将停止工作。