Pregunta

I'm just trying to solving this text-processing task using python, but I'm not able to compare column.

What I have tried :

#!/usr/bin/env  python
import sys

def     Main():
        print "This is your input Files %s,%s" % ( file1,file2 )
        f1 = open(file1, 'r')
        f2 = open(file2, 'r')

        for line in f1:
                column1_f1 = line.split()[:1]
                #print column1_f1
                for check in f2:
                        column2_f2 = check.split()[:1]
                        print column1_f1,column2_f2
                        if column1_f1 == column2_f2:
                                print "Match",line
                        else:
                                print line,check


        f1.close()
        f2.close()


if __name__ == '__main__':
        if len(sys.argv) != 3:
                print >> sys.stderr, "This Script need exact 2 argument, aborting"
                exit(1)
        else:
                ThisScript, file1, file2 = sys.argv
                Main()

I'm new in Python, Please help me to learn and understand this..

¿Fue útil?

Solución

I would resolve it in similar way in python3 that user46911 did with awk. Read second file and save its keys in a dictionary. Later check if exists for each line of first file:

import sys 

codes = {}

with open(sys.argv[2], 'r') as f2: 
    for line in f2: 
        fields = line.split()
        codes[fields[0]] = fields[1]

with open(sys.argv[1], 'r') as  f1: 
    for line in f1: 
        fields = line.split(None, 1)
        if fields[0] in codes:
            print('{0:4s}{1:s}'.format(codes[fields[0]], line[4:]), end='')
        else:
            print(line, end='')

Run it like:

python3 script.py file1 file2

That yields:

         060090 AKRABERG FYR                        DN  6138   -666     101
EKVG     060100 VAGA FLOGHAVN                       DN  6205   -728      88
         060110 TORSHAVN                            DN  6201   -675      55
         060120 KIRKJA                              DN  6231   -631      55
         060130 KLAKSVIK HELIPORT                   DN  6221   -656      75
         060160 HORNS REV A                         DN  5550    786      21
         060170 HORNS REV B                         DN  5558    761      10
         060190 SILSTRUP                            DN  5691    863       0
         060210 HANSTHOLM                           DN  5711    858       0
EKGF     060220 TYRA OEST                           DN  5571    480      43
EKTS     060240 THISTED LUFTHAVN                    DN  5706    870       8
         060290 GROENLANDSHAVNEN                    DN  5703   1005       0
EKYT     060300 FLYVESTATION AALBORG                DN  5708    985      13
         060310 TYLSTRUP                            DN  5718    995       0
         060320 STENHOEJ                            DN  5736   1033      56
         060330 HIRTSHALS                           DN  5758    995       0
EKSN     060340 SINDAL FLYVEPLADS                   DN  5750   1021      28
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top