我正在尝试将这个空格分隔的文本文件转换为列和行(我希望最终将其转换为JSON)。我的脚本可能无法区分col,主要是因为我在寻找空白。我无法更改输入的格式(文本文件传入)
我对文件的格式有问题,这是一个非常简单的例子
col1 col2 col3 text col4
1403 bash 2014-07-28 22:32:53 UTC+0000 sudo bash
1464 bash 2014-07-28 22:32:28 UTC+0000 sudo root
当我分析文件时,我会得到以下虚线下的数据:
^{pr2}$我希望它看起来像这样:
['1403', 'bash', '2014-07-28 22:32:53 UTC+0000', 'sudo bash']
这是一个非常基本的例子。但基本上我是把标题映射到破折号下面的数据。如果你能帮忙,请告诉我。在
注意:输出不必完全如图所示,我只想能够分离cols数据。在
以下是到目前为止我在代码中的内容,它将头分离到各个col:
colNames = testFile.readline()
#tempList = re.split('(\s )', colNames)
headerList = []
for i in tempList:
j = i.replace(' ','')
if j != '':
headerList.append(j)
然后我有一个循环,根据数据的位置遍历数据(我认为我需要找到一种更好地分离数据的方法):
for line in testFile.readlines():
if rowCounter > 0:
row = line.split()
print row
for i in range(len(headerList)):
colNameDic.update({headerList[i]:row[i]})
rowDic = dict(colNameDic)
fileList.append(rowDic)
rowDic = {}
rowCounter +=1
这里有一种替代方法:对文本行进行两次处理。在第一步中,我们将猜测起始索引:非空格字符前面有空格的索引。对于每一行,开始索引可能稍有不同,但是如果我们看一下这些索引,它们都是全部共同的,那么这些索引可能是列的开始。对启动指数的猜测并不完美。它要求一行中没有一个单元格丢失
在第二个步骤中,我们将使用这些索引将一行拆分为列,去掉后面的空白。在
注释
first_pass
和{只要列宽是固定的,就可以使用固定宽度的解析器:
如果标题行在列名称中没有外部空格,那么可以使用
slice
内置函数拆分数据行(请注意,我在标题行中添加了一个下划线以规范化列的名称)PS您可能想
rstrip
个别项目,[r[s].rstrip() for s in slices]
。在附录
如果有可能在有效列名和垃圾之间找到区别,那么可以放宽无外来空间要求。。。重新设置您的特定数据格式(无下划线…)
^{pr2}$相关问题 更多 >
编程相关推荐