Leer archivo TXT desde Python

#!/usr/bin/env python # -*- coding: utf-8 -*- #Python 3 class Extractor: ruta = '' def __init__(self): print("objeto Extractor creado e inicializado") def __str__(self): return "Extrator{ruta:"+self.ruta+"}" path='C:\\Users\\DELL\\Documents\\archivo.txt' def recorrer(string): separador = None lista=[] datos = Datos('','','','') print("Has entrado a recorrer") print("Tenemos:"+string) separador = string.split(",") datos.correo=separador[0] datos.apellidos=separador[1] datos.nombre=separador[2] datos.fecha=separador[3] print(datos) lista.append(datos) print("Lista:"+str(lista)) for l in lista: print(l.nombre) print(l.apellidos) print(l.fecha) print(l.correo) def main(): global path extractor = Extractor() file,contador = None, 0 lista=[] try: extractor.ruta = path if extractor.ruta!='' or extractor.ruta!=None: print(extractor.ruta) file = open(extractor.ruta,'r') if file==None: print("Archivo no existente!") else: print("Archivo existente!") try: #print("Contenido:"+file.readline()) for f in file: #print(f+" : "+str(contador)) contador = contador +1 recorrer(f) except FileNotFoundError as noexiste: print("No se encuentra el archivo:"+noexiste) except IOError as error: print("Ha ocurrido un error:"+error) if __name__ == "__main__": main()
Lectura de archivo TXT

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.