使用python转换空格分隔的文本文件?

2024-09-28 22:05:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将这个空格分隔的文本文件转换为列和行(我希望最终将其转换为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

Tags: 文件数据inbashfor格式sudocol
3条回答

这里有一种替代方法:对文本行进行两次处理。在第一步中,我们将猜测起始索引:非空格字符前面有空格的索引。对于每一行,开始索引可能稍有不同,但是如果我们看一下这些索引,它们都是全部共同的,那么这些索引可能是列的开始。对启动指数的猜测并不完美。它要求一行中没有一个单元格丢失

在第二个步骤中,我们将使用这些索引将一行拆分为列,去掉后面的空白。在

import itertools

def split_columns(row, start_indices):
    start_indices = start_indices + [None]
    a, b = iter(start_indices), iter(start_indices)
    next(b)

    columns = []
    for istart, istop in zip(a, b):
        columns.append(row[slice(istart, istop)].strip())

    return columns

def guess_start_indices(row):
    row = ' ' + row
    prev_seq, cur_seq = iter(row), iter(row)
    next(cur_seq)

    start_indices = []
    for i, (prev, cur) in enumerate(zip(prev_seq, cur_seq)):
        if prev == ' ' and cur != ' ':
            start_indices.append(i)
    return set(start_indices)

def find_common_start_indices(rows):
    start_indices = set.intersection(*(guess_start_indices(row) for row in rows))
    start_indices = sorted(start_indices)
    return start_indices

if __name__ == '__main__':
    with open('columnize.txt') as rows:
        first_pass, second_pass = itertools.tee(rows)

        start_indices = find_common_start_indices(first_pass)
        print(start_indices)

        for row in second_pass:
            print(split_columns(row, start_indices))

注释

  • 在代码中,我创建了两个迭代器first_pass和{}来帮助迭代文本行。这些迭代器很重要,因为它们允许在不倒带文件的情况下对行进行两次迭代。在
  • 这种方法的主题是猜测,所以会有文本愚弄编码,它会做出错误的猜测
  • 因此,将此解决方案作为起点并验证输出

只要列宽是固定的,就可以使用固定宽度的解析器:

pd.read_fwf(io.StringIO(data), skiprows=[1], 
            parse_dates=["col3 text"], # Optional
            colspecs=[(0,4),(5,25),(26,54),(57,66)])
#   col1  col2            col3 text       col4
#0  1403  bash  2014-07-28 22:32:53  sudo bash
#1  1464  bash  2014-07-28 22:32:28  sudo root

如果标题行在列名称中没有外部空格,那么可以使用slice内置函数拆分数据行(请注意,我在标题行中添加了一个下划线以规范化列的名称)

In [20]: h = 'col1 col2                 col3_text                      col4\n'
In [21]: r = '1403 bash                 2014-07-28 22:32:53 UTC+0000   sudo bash\n'
In [22]: fields = h.split()
    ...: ifields = [h.index(f) for f in fields]+[None]
In [23]: slices = [slice(i,j) for i, j in zip(ifields, ifields[1:])]
In [24]: tokens = [r[s] for s in slices]
In [25]: tokens
Out[25]: 
['1403 ',
 'bash                 ',
 '2014-07-28 22:32:53 UTC+0000   ',
 'sudo bash\n']
In [26]: 

PS您可能想rstrip个别项目,[r[s].rstrip() for s in slices]。在


附录

如果有可能在有效列名和垃圾之间找到区别,那么可以放宽无外来空间要求。。。重新设置您的特定数据格式(无下划线…)

^{pr2}$

相关问题 更多 >