Efficient way to edit text tabular file so each cell starts at the same position

Related searches

I have a text file of table-like structure, each line contains 0 to 4 words split by arbitrary number of spaces.

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

My goal is to edit this file in a format where elements start at the same position across lines, for example:

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

The number of spaces is arbitrary. I prefer the lines starting with a space skips the first element, but this is not strict.

I believe there are a lot of ways to do this, my preference order is:

  1. On vim with some neat trick
  2. By bash command
  3. On a text editor with such functionality
  4. By script language (perhaps python)

Since this is a part of data prep/validation process, I do not need a perfect method; I will conduct manual check after all. I am looking for a way that does, say, 80 to 90% of the work.

Can someone suggest an efficient approach?

If useful, example file is here.

Here's a way to get column to respect leading whitespace: change a leading space to some other character

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

Formatting tables, Use these dialog boxes to change the number of rows and columns, to change the Position the pointer over the edge of a column or row so that a of the paragraph in the first cell, press the Left Arrow key, and begin typing. In general , use the same methods to format text in a table that you would use to� If you want a quick way to write same text in same cell position then this article is for you. In this article, we will learn how to loop through in all the worksheets & add similar text in same position. Question): I have 3 sheets in a workbook & I want a macro to enter text in all the sheets in cell A1. We need to follow the below steps:

Python's re module, .format() offer a good approach to 4..

The column width is based on the length of the longest non-whitespace string in your file + column_pad value.

You can play around with column_pad to vary the actual column width.

If you pass in rename_file=True, you'll get a new file named 'cleaned_<filename>filename`. Otherwise, the script will replace the original file with the cleaned file.

#!/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)

Output:

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

Selecting and editing tables, Click inside a cell, or select text, and then choose Table > Select > Column You can also select a table in the same way you select an anchored graphic—place the You can insert rows and columns using a number of different methods. the Hand tool appears—so make sure you begin dragging before� Follow these steps to align text in a table: Select the cells, columns, or rows, with text that you want to align (or select your entire table). Go to the (Table Tools) Layout tab. Click an Align button (you may have to click the Alignment button first, depending on the size of your screen).

Model for Tabular Data and Metadata on the Web, This document aims to primarily satisfy the "Access methods for CSV Each row contains the same number of cells (although some of these cells may be value of the text direction annotation on columns and cells within the table, the position of the column in the file at the url of the table, starting from 1,� By using the same unique character to divide text, you can define how you want Word to define how much text to display in each individual cell of a table. To convert existing text into a table, follow these steps: Click the Insert tab. Select the text that you want to convert into a table. Click the Table icon. A pull-down menu appears. Click

Column Mode in UltraEdit, Learn all about how you can edit columns of data in seconds, and explore UltraEdit will highlight text based on the column position of the first character you You can configure a custom font for column mode so that it's obvious when the editor If there is no text data at the specified column on a row within the file, spaces� As Z is specified, the content of the cells is also centered. Alternatively, X would left-align and Y right-align. You can use p{1.5cm} or so to have a specific column stretch to a predefined size. As long as you use any of X, Y, Z together with it, the table will stretch to the width of your text margin.

Paste like a Pro, Use Excel Paste Special to copy and paste data in creative ways. So he wanted you to create another table to track sales and you want to pasting live xml data to your sheets, pasting images, objects, files etc. Get started with Power BI Select a cell, go to Edit menu > Text To Columns, Delimiters,� Getting started. The algorithm consists of three parts: the first is the table detection and cell recognition with Open CV, the second the thorough allocation of the cells to the proper row and column and the third part is the extraction of each allocated cell through Optical Character Recognition (OCR) with pytesseract.

I have a text file, where different forms of content exist, data or strings or symbols, i need to change a specific line (it's only a number), i know its position (69 line) Is there any way to do so? 0 Comments

Comments
  • column -t file (or :%!column -t from within vim) does what you want, except for the "right aligned" (ie fill with space first) part. I don't know of any easy/straightforward way to do that last part though.
  • @Marth This is very helpful.
  • Thanks for a vim solution. I will try this out.