Arrange word in table metrix format


 
Thread Tools Search this Thread
Top Forums Programming Arrange word in table metrix format
# 1  
Old 02-07-2013
Arrange word in table metrix format

Hello everyone,

I have some problem about this code :
Code:
#!/usr/bin/env python
import sys

try :
   filename = sys.argv[1]
except :
   print 'Specify filename'
   sys.exit()
fd = open(filename)
lines = fd.xreadlines()
compare = {}
for line in lines :
    split_line = line.strip().split('\t')
        if compare.has_key(split_line[0]) :
            for i in range(1,3) :
                try :
                    compare[split_line[0]].index(split_line[i])
                except :
                    compare[split_line[0]].append(split_line[i])
        else :
            compare[split_line[0]] = []
            for i in range(1,3) :
                try :
                    compare[split_line[0]].index(split_line[i])
                except :
                     compare[split_line[0]].append(split_line[i])
fd.close()
for i in compare.keys() :
   print '%s\t%s' %(i,compare[i])

Input file is
Quote:
SN1 data1 A,A
SN1 data2 A,B
SN1 data3 A,C
AC2 data1 A,B
AC2 data2 A,C
TP3 data3 C,C
TP3 data1 C,A
Output from this code showed :
Quote:
AC2 ['data1', 'A,B', 'data2', 'A,C']
TP3 ['data3', 'C,C', 'data1', 'C,A']
SN1 ['data1', 'A,A', 'data2', 'A,B', 'data3', 'A,C']
If I would like get the output file in below :
Quote:
AC2 data1 A,B data2 A,C data3 N,N
TP3 data1 C,A data2 N,N data3 C,C
SN1 data1 A,A data2 A,B data3 A,C
Expected output should be arranged in data1 to data3 in every row. If any column is loss, the data will show in N,N.

I don't know that how to solve this problem, please suggest me.
Thank in advance.
# 2  
Old 02-10-2013
Quote:
Originally Posted by awil
...
Input file is
Code:
SN1     data1   A,A
SN1     data2   A,B
SN1     data3   A,C
AC2     data1   A,B
AC2     data2   A,C
TP3     data3   C,C
TP3     data1   C,A

...
If I would like get the output file in below :
Code:
AC2    data1 A,B data2 A,C data3 N,N
TP3     data1 C,A data2 N,N data3 C,C 
SN1     data1 A,A data2 A,B data3 A,C

Expected output should be arranged in data1 to data3 in every row. If any column is loss, the data will show in N,N.
I don't know that how to solve this problem, please suggest me.
...
I don't quite understand your implementation in Python, but I do understand your problem. A suggested algorithm is as follows:

Code:
(1) Read a line, split it by Tabs and then determine if the first token exists as a hash key.
(2) If it does not, then assign a "template" array that has "N,N"s at all the right places.
    The template array is this: ("data1", "N,N", "data2", "N,N", "data3", "N,N").
(3) From the 2nd token, determine the index of the template array that you want to update.
    So, for example, if the 2nd token is "data1", you extract 1 from it and you know that the "N,N" after "data1" is to be updated.
(4) Update the template array at the relevant index.

I've implemented this algorithm in Perl, and it is posted below.

A few notes:
(1) A hash value is expected to be a scalar in Perl, so you set the "reference" to an array as the hash value. A "reference" in Perl is similar to a "pointer" in C. No clue what it is called in Python, or if you are using that in your code.

(2) Perl has 0-based arrays i.e. the first index of an array is 0. (It appears that Python array indexes start with 1, by looking at your code.) So, once you read the second token, say, "data3", and extract 3 from it, then you'll have to update index 5 (=2*3 - 1) of the template array.

Code:
- Read "data1" -> extract 1 -> update index 2*1 - 1 = 1 of array ("data1", "N,N", "data2", "N,N", "data3", "N,N")
- Read "data2" -> extract 2 -> update index 2*2 - 1 = 3 of array ("data1", "N,N", "data2", "N,N", "data3", "N,N")
- Read "data3" -> extract 3 -> update index 2*3 - 1 = 5 of array ("data1", "N,N", "data2", "N,N", "data3", "N,N")

(3) I've adopted this "hard-coded" template array approach because I see this in your code:
Quote:
Originally Posted by awil
Code:
            ...
            for i in range(1,3) :
            ...

which makes me believe that a particular token "SN1", or "AC2" or "TP3" can have at the most three records. If that's not the case, then the problem becomes more interesting!

By this approach, once you are done reading the file, your data structure is ready and you can simply print off the results. I hope the script comments are sufficient.

Code:
$
$ # check the data file
$ cat -n test.txt
     1  SN1     data1   A,A
     2  SN1     data2   A,B
     3  SN1     data3   A,C
     4  AC2     data1   A,B
     5  AC2     data2   A,C
     6  TP3     data3   C,C
     7  TP3     data1   C,A
$
$ # check the program file
$ cat -n process_test.pl
     1  #!/usr/bin/perl
     2  use strict;
     3  use warnings;
     4
     5  die "Specify filename\n" if not defined $ARGV[0];       # Ask for filename
     6  my %compare;                                            # Declare the hash to store all information
     7  my $file = $ARGV[0];                                    # Assign the filename to a variable
     8  open (FH, "<", $file) or die "Can't open $file: $!";    # Open the file handle; balk on error
     9  while (<FH>) {                                          # Loop through the file, line by line
    10    chomp;                                                # Remove the End-of-Line character
    11    my @tokens = split/\t+/;                              # Split line on Tab and assign to array "tokens"
    12    if (not defined $compare{$tokens[0]}) {               # If 1st element of "tokens" is not a key, then
    13      $compare{$tokens[0]} = [ "data1", "N,N",            # Create the key in the "compare" hash and
    14                               "data2", "N,N",            # assign a template value with default "N,N"s
    15                               "data3", "N,N"             # The [] returns a reference to the array, since
    16                             ];                           # the hash value must be a scalar in Perl.
    17    }
    18    (my $index = $tokens[1]) =~ s/\D+//;                  # Determine the array index to be updated
    19    $compare{$tokens[0]}->[2*$index-1] = $tokens[2];      # And then update the array
    20  }                                                       # Done reading the file
    21  close (FH) or die "Can't close $file: $!";              # So close it; balk on error
    22  while (my ($k, $v) = each %compare) {                   # Loop through the hash
    23    printf ("%s %s\n", $k, join (" ", @{$compare{$k}}));  # and print out the keys and values
    24  }
$
$ # A dry run
$ perl process_test.pl
Specify filename
$
$ # A successful run
$ perl process_test.pl test.txt
AC2 data1 A,B data2 A,C data3 N,N
TP3 data1 C,A data2 N,N data3 C,C
SN1 data1 A,A data2 A,B data3 A,C
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output in table format

I have one script which generate file called report.txt having following output parameter_name status comment banking ok NA finance 30% hike NA Loan_department ok 20% HR_Group defaulters Ajay I wanted to convert this file into tabular form. You can see each line contain 3 words and... (7 Replies)
Discussion started by: Nakul_sh
7 Replies

2. Shell Programming and Scripting

Arrange data in table

Hello All, I have following data into my file named record. Name City phone number email Jhon Newyork 123456987 jhon@gmail.com Maria Texas 569865612 Maria_Sweet@rediffmail.com Chan Durben NA Chan123@gmail.com The output should be in straight columns.. There should not be any... (1 Reply)
Discussion started by: Nakul_sh
1 Replies

3. Shell Programming and Scripting

Arrange data in table

Hello All, I have following data into my file named record. Name City phone number email Jhon Newyork 123456987 jhon@gmail.com Maria Texas 569865612 Maria_Sweet@rediffmail.com Chan Durben NA Chan123@gmail.com |---------------------------------------------------------------| |Name ... (2 Replies)
Discussion started by: Nakul_sh
2 Replies

4. Shell Programming and Scripting

How to words in arrange as Table

Hi Team, I have one file in that almost 100+ words in lines Eg:- Unix windows solaris Linux ... ... But I want arrange all lines in table format and can able read on screen Eg: - Unix windows solaris Lunix...... Hp unix Mac-os ...... Like as Table...... (11 Replies)
Discussion started by: Bhaskar Alagala
11 Replies

5. Shell Programming and Scripting

script to arrange file in specific format

Hi All, I am new to forum, I am looking to arrange a file in specific format but unable to get the formula to do it, already googled for the same, but didnt find the answer :(. hope to get help here :o:o:o:o:o I have to files : $ cat Dev_List2 0685 0686 0687 0688 0689 068A 068B 068C... (2 Replies)
Discussion started by: prasan_Aix
2 Replies

6. Shell Programming and Scripting

Table format

My Code Hi Friends, I need to get output in table format using unix shell script.For example my server CPU and memory utilization report will come as a mail with ordinary format but i want to make as table format. Here two output finally we are getting:- CPU utilization is... (2 Replies)
Discussion started by: susindram
2 Replies

7. Shell Programming and Scripting

Arrange / format data using awk

Input 217:fngadi4osa:fngadi4osa:M 217:415744:N/A 227:fngadi4osa:fngadi4osa: M 227:51200:N/A 228:fngadi4osa:fngadi4osa: M 228:102400:N/A 65:sapgt04:sapgt04: M 65:104448:N/A 228:fngadi4osa:fngadi4oma: M 228:102400:N/A Output 217:fngadi4osa:fngadi4osa:M 217:415744:N/A... (3 Replies)
Discussion started by: greycells
3 Replies

8. Programming

Create table with date format.

Hello, Could you please let me know the correct format of CREATE TABLE statement with DATE format. CREATE TABLE EMP_TABLE1 ( NAME VARCHAR(6) PRIMARY KEY, ADDRESS VARCHAR(6), BIRTH_DATE DATE ); I want BIRTH_DATE to be in "YYYYMMDDHHMISS" format. How we can create table with... (4 Replies)
Discussion started by: Poonamol
4 Replies

9. Shell Programming and Scripting

Arrange Data in table and send by mail

Everybody, can you tell me how express about this; we have text data file as : parameter1 Parameter2 AA 55 BB 77 . . . . . . We want to draw table for this data as attached then send as body of Email (4 Replies)
Discussion started by: xjklop2009
4 Replies

10. Shell Programming and Scripting

Format & re-arrange the records

Data on my input file : Ac1n1s1c2n2s2XPd1r1e1t1d2r2e2t2d3r3e3t3d4r4e4t4RT Bh1k1p1h2k2p2NTq1y1f1m1q2y2f2m2q3y3f3m3q4y4f4m4ZN and i want the output to be: Ac1n1s1XPd1r1e1t1RT Ac1n1s1XPd2r2e2t2RT Ac1n1s1XPd3r3e3t3RT Ac1n1s1XPd4r4e4t4RT Ac2n2s2XPd1r1e1t1RT Ac2n2s2XPd2r2e2t2RT... (6 Replies)
Discussion started by: rlmadhav
6 Replies
Login or Register to Ask a Question