Juntando tabelas em aquivo texto

Publicado por Carlos Eduardo de Andrade 25/10/2007

[ Hits: 6.524 ]

Download merge_columns.py




Trabalhar com várias tabelas, juntando e combinando resultados, é uma tarefa demorada, chata e, às vezes, dolorosa. Ainda mais se estas estiverem em modo texto, como arquivos CVS ou algo do gênero.

Há algum tempo atrás fiz um script para me ajudar nesta tarefa. Ele precisa ser melhorado, mas já me ajudou bastante. Espero que possam aproveitar também, principalmente os usuários de latex de plantão.

Aceito sugestões ;-)

  



Esconder código-fonte

#!/bin/env python
# -*- coding: iso-8859-1 -*-
"""
merge_columns.py: merge table by their columns.

See <help_msg> for more information.

(c) Carlos Eduardo de Andrade. April, 2006.
"""


usage_msg = "Usage:\nmerge_columns.py [-is=<initial_separator>] \
[-fs=<final_separator>] [-el=<end_of_line>] [-fc=<fix_col_1,fix_col_2,...>] \
col_1,col_2,...,col_n in_file_1 in_file_2 ... in_file_m out_file\n\n\
NOTE: columns are separeted by comma (,)."

help_msg = usage_msg + "\n\n--\
This script merge text tables choosing the columns that must be side by side.\
 For example, the command\n\n\
merge_columns.py -fc=0 1,2 table1.txt table2.txt final_table.txt\n\n\
table1.txt:\n\
Mary & 4 & 5\n\
John & 1 & 6\n\
Paul & 4 & 5\n\n\
table2.txt:\n\
Mary & 7 & 9\n\
John & 3 & 5\n\
Paul & 2 & 2\n\n\
final_table.txt (with columns 1 and 2)\n\
Mary & 4 & 7 & 5 & 9 \\\\\n\
John & 1 & 3 & 6 & 5 \\\\\n\
Paul & 4 & 2 & 5 & 2 \\\\\n\n\
Optinal arguments are:\n\
[-is=<initial_separator>] = Column separator in input files. Default: \"&\"\n\
[-fs=<final_separator>] = Column separator in output file. Default: \"&\"\n\
[-el=<end_of_line>] = End of line. Default: \"\\\\\"\n\
[-fc=<fix_col_1,fix_col_2,...>] = Select colunms that must appear \
once. The columns are taken from first in_file.\n\n\
(c) Carlos Eduardo de Andrade. April, 2006."

import sys

initial_separator = "&"
final_separator = "&"
end_of_line = "\\\\"
fixed_columns = []

try:
    if len(sys.argv) == 1:
        print help_msg
        sys.exit(0)

    # Take the optional args.
    remove_args = []
    for arg in sys.argv:
        remove = True

        if arg.find("-is=") != -1:
            initial_separator = arg[4:]
        else:
            if arg.find("-fs=") != -1:
                final_separator = arg[4:]
            else:
                if arg.find("-el=") != -1:
                    end_of_line = arg[4:]
                else:
                    if arg.find("-fc=") != -1:
                        fixed_columns = map(int,arg[4:].split(","))
                    else:
                        remove = False

        if remove:
            remove_args.append(arg)
    #endfor

    # Remove optional args
    for arg in remove_args:
        sys.argv.remove(arg)

    # Verifies the correct arguments number.
    if len(sys.argv) < 4:
        print usage_msg
        sys.exit(2)


    # Take all columns
    columns = map(int, sys.argv[1].split(","))

    lines_number = 0;

    # Open the files
    files = []
    for i in range(2, len(sys.argv) - 1):

        raw_lines = open(sys.argv[i]).readlines()

        # Parse the lines
        lines = []
        for col in raw_lines:
            lines.append(col.split(initial_separator))
        #endfor

        if lines_number == 0:
            lines_number = len(lines)
        else:
            if lines_number != len(lines):
                print "\nFiles have a different lines number:",
                print sys.argv[i - 1] + " " + sys.argv[i]
                print "Perhaps the files contain a different number of blank",
                print "lines at end."
                sys.exit(1)

        files.append(lines)
    #endfor

    file_out = open(sys.argv[-1], 'w')

except ValueError:
    print usage_msg
    sys.exit(1)
except IOError, msg:
    print msg
    sys.exit(1)

# Mount the new table
try:
    # For each line
    for line in range(0, len(files[0])):

        # First, we take the fixed columns
        if len(fixed_columns) != 0:
            index = fixed_columns[0]

            if len(fixed_columns) > 1:
                for col in fixed_columns[:-1]:
                    file_out.write(files[0][line][col] + final_separator)

                index = len(fixed_columns) - 1
            #endif
            file_out.write(files[0][line][index])
        #endif

        # For each column
        for column in columns:
            # For each file
            for file in files:
                file_out.write(final_separator + file[line][column])
            #endfor
        #endfor

        file_out.write(end_of_line + "\n")
    #endfor
except IndexError:
    print "Invalid Column: %d" % (column)
    sys.exit(1)

Scripts recomendados

backup all files

Miojo Notepad 0.3

Agenda de compromissos

Serialização em Python usando pickle

Mini-dicionário de termos em Python e PyQT 4


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts