Created
May 29, 2019 17:20
-
-
Save nosrednawall/0da50e72f02d5359f2d9b0a964dc21e1 to your computer and use it in GitHub Desktop.
Ler arquivos em pastas com python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nome | valor | |
---|---|---|
camiseta | 25.0 | |
jaqueta | 125.0 | |
tenis | 80.0 | |
bermuda | 40.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nome, valor camiseta, 25.0 jaqueta, 125.0 tenis, 80.0 bermuda, 40.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import os | |
class Produto(object): | |
def __init__(self, nome, valor): | |
self.__nome = nome | |
self.__valor = valor | |
@property | |
def nome(self): | |
return self.__nome | |
@nome.setter | |
def nome(self, nome): | |
self.__nome = nome | |
@property | |
def valor(self): | |
return self.__valor | |
@valor.setter | |
def valor(self, valor): | |
self.__valor = valor | |
def __repr__(self): | |
return "nome:%s valor:%s" % (self.__nome, self.__valor) | |
def verificar_arquivo(): | |
caminho = '/home/anderson/scripts_python/' | |
arquivo = caminho + 'dados.csv' | |
if not os.path.exists(caminho): | |
os.mkdirs(caminho) | |
elif not os.path.isdir(caminho): | |
raise IOError(caminho + "Nao eh um diretorio!") | |
if not os.path.exists(arquivo): | |
open(arquivo, 'w') | |
return arquivo | |
def ler_produtos(arquivo): | |
arquivo_aberto = open(arquivo, 'rb') | |
return csv.reader(arquivo_aberto,delimiter=',') | |
arquivo = verificar_arquivo() | |
dados = ler_produtos(arquivo) | |
next(dados) | |
#print [dado for dado in dados] | |
produtos = [Produto(dado[0], dado[1]) for dado in dados] | |
print produtos | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment