Process a TXT to PRN with Python

0

I have several txt files with information on some payments in txt separated by line breaks and slashes. What I have is this:

{

#!/usr/bin/env python


# Importacion de la libreria necesaria
import re

#Apertura de archivo
fname = raw_input(": >")
fopen = open(fname) 


#Se definen 3 listas, una para cada columna
col1 = list()
col2 = list()
col3 = list()

#Edicion del texto para cada campo

for i in fopen:
    col = re.findall('[0-9]+', i)
    col1.append(col[0].lstrip('0'))
    col2.append(col[1].lstrip('0'))
    decimal = col[2] + "." + col[3]
    col3.append(decimal)
fopen.close()

#Impresion de archivo PRN
cont = 0
while cont < len(col1):
   prn = col1[cont] + " " + col2[cont] + " " + col3[cont] + "\n"
   print prn
   cont = cont + 1 
   fwrite.write(prn)

}

The text is as I want, what I want to do is save a PRN file (text with spaces with a maximum length of 240 characters per line) directly with python with the aforementioned format.

{

   3830762       403  2620.39
   3830762       404  2620.39
   3830762       216  3226.30
   3975560       217  1329.61
   3975560       210  2124.30
   3975560       403  3351.33
   3975560       404  3351.33
   4068493       403  2922.72
   4068493       404  2922.72
   4378091       210  1028.15
   4378091       403  2337.48
   4378091       404  2337.48
   4404173       216  1001.80
   4527545       403  2227.71
   4527545       404  2227.71
   4975179       216   824.10
   4975179       403  2254.68
   4975179       404  2254.68
   5048485       403  2643.13
   5048485       404  2643.13
   5060558       403  3516.09
   5060558       404  3516.09
   5181225       403  3498.39
   5181225       404  3498.39

}

    
asked by Bryan Useche 13.09.2017 в 20:13
source

1 answer

0

Using the simplest and safest way to save a file:

with open("archivo.prn", "w") as file
   while cont < len(col1):
      prn = col1[cont] + " " + col2[cont] + " " + col3[cont] + "\n"
      print prn
      cont = cont + 1 
      file.write(prn)

We use what is known as context managers an own construction of Python that in the case of the files allows to close them automatically, once finalized all the block of instructions that contains, besides providing a cleaner and safe way to finish in case of error.

On the other hand, rewriting your code with context managers for reading and writing and in a more "pythonica" way, it could look like this:

import re

fname = raw_input(": >")
with open("{0}.prn".format(fname), "w") as out_file:
    with open(fname, "r") as in_file:
        for linea in in_file.readlines():
            col = re.findall('[0-9]+', linea)
            out = "{0} {1} {2}.{3}\n".format(col[0].lstrip('0'), col[1].lstrip('0'), col[2], col[3])
            out_file.write(out)

Doing in_file.readlines() , we read all the lines of the file and for each one, we are doing the process and saving it immediately, with this we reduce the logic enough. As you can see when using the context manager we do not need to do the usual close .

    
answered by 14.09.2017 / 05:14
source