代码之家  ›  专栏  ›  技术社区  ›  Kota Mori

编辑文本表格文件的有效方法,使每个单元格从同一位置开始

  •  0
  • Kota Mori  · 技术社区  · 7 年前

    我有一个表格结构的文本文件,每行包含0到4个单词,由任意数量的空格分割。

    hello     world  this  is
         an   example  file
    is   there a   good
    way to    clean this
      your help is   
    highly      appreciated
    

    我的目标是以一种格式编辑此文件,其中元素跨行从同一位置开始,例如:

    hello    world        this     is
             an           example  file
    is       there        a        good
    way      to           clean    this
             your         help     is       
    highly   appreciated
    

    空格的数量是任意的。我更喜欢以空格开头的行跳过第一个元素,但这并不严格。

    1. 用一些巧妙的技巧在vim上
    2. 通过bash命令
    3. 在具有此类功能的文本编辑器上
    4. 通过脚本语言(可能是python)

    由于这是数据准备/验证过程的一部分,我不需要一个完美的方法;毕竟我会进行人工检查。我正在寻找一种方法,比如说,完成80%到90%的工作。

    如果有用,示例文件是 here .

    0 回复  |  直到 7 年前
        1
  •  3
  •   glenn jackman    7 年前

    这里有一个方法 column 尊重前导空格:将前导空格更改为其他字符

    sed 's/^ /_ /' file | column -t | sed 's/^_ /  /'
    
    hello   world        this     is
            an           example  file
    is      there        a        good
    way     to           clean    this
            your         help     is
    highly  appreciated
    
        2
  •  2
  •   Chris Larson    7 年前

    re 单元 .format() 提供一个很好的解决方案 4. .

    column_pad 价值

    你可以和我一起玩 柱垫 更改实际列宽。

    如果你过去 rename_file=True ,您将获得一个名为 'cleaned_<filename> 文件名“”。否则,脚本将用清理后的文件替换原始文件。

    #!/usr/bin/env python
    import re
    import sys
    
    def clean_columns(filename, rename_file=False, column_pad=4):
        if rename_file:
            cleaned_filename = 'cleaned_' + filename
        else:
            cleaned_filename = filename
    
        cleaned_text = ''
    
        with open(filename, 'r') as dirty_file:
            text = dirty_file.readlines()
    
        string_list = list(
            {string.strip()
                    for line in text
                    for string in line.strip().split(' ')})
    
        max_string_length = len(max(string_list, key=len))
        column_width = max_string_length + column_pad
        formatting_string = '{: <' + str(column_width) + '}'
    
        for line in text:
            line = re.sub(r'\s+',' ', line).split(' ')
            formatting = formatting_string * len(line)
            line = formatting.format(*line)
            cleaned_text += line + '\n'
    
        with open(cleaned_filename, 'w') as cleaned:
            cleaned.write(cleaned_text)
    
    
    clean_columns('sample.txt', rename_file=True, column_pad=8)
    

    输出:

    hello              world              this               is
                       an                 example            file
    is                 there              a                  good
    way                to                 clean              this
                       your               help               is
    highly             appreciated
    
        3
  •  2
  •   SolaWing    7 年前

    https://github.com/junegunn/vim-easy-align 用于对齐各种分隔符的插件

    只需选择行,按:

    • <CR> :映射到 <Plug>(EasyAlign)
    • <C-P>
    • * :对齐所有分隔符
    • <C-D> :切换直到左对齐分隔符
    • <C-X>\s\@<=\S\+ :选择空格后的非空格作为分隔符

    或使用以下命令: '<,'>EasyAlign */\s\@<=\S\+/dl

    推荐文章