Advertisement
AntonioVillanueva

Load & process File

Aug 26th, 2022
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """ Antonio Villanueva Segura
  5.     Load external txt file, process it
  6.     and create a  filtered pos. list
  7. """
  8.  
  9. class lista ():    
  10.     liste =[] #uploaded  string file
  11.    
  12.     def __init__(self,filename):
  13.         self.openFile(filename)
  14.    
  15.     def listRange(self,liste,first, last):
  16.         """Show a range of data from the list  """
  17.         for listLine in self.liste: #Loop over liste
  18.             print ( listLine[first],listLine[last] )
  19.  
  20.     def creeListe(self,line):
  21.         """ Create and filter a list from a  string line """
  22.         tmp='' #New filtered line
  23.         last='' #last character
  24.         """Filter process line """
  25.         for c in line:
  26.             if c.isprintable() :
  27.                 tmp+=c
  28.             elif last != c:
  29.                 tmp+='\t'
  30.             last=c
  31.        
  32.         return tmp.split ('\t')
  33.  
  34.     def openFile (self,filename):  
  35.         """ load File to dataFile """
  36.         with open(filename) as file:
  37.             for line in file:
  38.                 if (line.strip()):#Not empty
  39.                     self.liste.append(self.creeListe(line))
  40.  
  41.     def run (self):
  42.         """ loop or main entry"""
  43.         self.listRange (self.liste,1,2)
  44.                
  45. if __name__ == "__main__":
  46.    
  47.     l=lista("configMqtt.txt")
  48.     l.run()
  49.    
  50.  
  51. #Example configMqtt.txt
  52. """
  53. ACS-Report-LED  2   01D48089    70b3d56371d48089    Report d’état lumineux  
  54. ACS-Report-LED  2   01D480E3    70b3d56371d480e3    Report d’état lumineux  
  55.  
  56. ACS-Report-Note 6   0188844F    70b3d5637188844f
  57.  
  58. ACS-Switch      3   01D431B6    70b3d56371d431b6
  59.  
  60. ACS-Switch-BUZZ 5   01D4B67A    70b3d56371d4b67a
  61.  
  62. ACS-Switch-PIR  4   01D3E040    70b3d56371d3e040    Détecteur d’arrivée de véhicule
  63.  
  64. ACS-Switch-TOF  1   01D43712    70b3d56371d43712    Capteur à la place
  65. ACS-Switch-TOF  1   01D43F44    70b3d56371d43f44    Capteur à la place
  66.                
  67. INS-Blueread    7   01D43153    70b3d56371d43153
  68. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement