28
Lendo dados tabulares: Versão Pythônica Turicas aka Álvaro Justen XI PythonBrasil 10 de novembro de 2015, São José dos Campos/SP

[PyBr11] Lendo dados tabulares: versão pythônica

Embed Size (px)

Citation preview

Page 1: [PyBr11] Lendo dados tabulares: versão pythônica

Lendo dados tabulares:Versão Pythônica

Turicas aka Álvaro JustenXI PythonBrasil

10 de novembro de 2015, São José dos Campos/SP

Page 4: [PyBr11] Lendo dados tabulares: versão pythônica

Patrocinadores sãoimportantes, mas retirar voz da

comunidade para

dar voz a quem já tem voz no momento mais democrático

do evento não é legal! =/

Page 5: [PyBr11] Lendo dados tabulares: versão pythônica

#temqueverissoaíBora discutir no bar depois! =)

Page 6: [PyBr11] Lendo dados tabulares: versão pythônica

CSV

Page 7: [PyBr11] Lendo dados tabulares: versão pythônica

#comofaz? #csvimport csv

reader = csv.DictReader(open('tesouro-direto.csv'))# TODO: identificar dialeto do CSV

for row in reader: print row # valores são strings. STRINGS! # TODO: fazer conversor de datetime # TODO: fazer conversor de date # TODO: fazer conversor de decimal # TODO: fazer conversor de percent

Page 8: [PyBr11] Lendo dados tabulares: versão pythônica
Page 9: [PyBr11] Lendo dados tabulares: versão pythônica

HTML

Page 10: [PyBr11] Lendo dados tabulares: versão pythônica

HTML

Page 11: [PyBr11] Lendo dados tabulares: versão pythônica

"A vida não é a porra do teu Toddynhogelado, não!"

Page 12: [PyBr11] Lendo dados tabulares: versão pythônica

#comofaz?#html

pip install lxml

import lxml.etree

filename = 'tesouro-direto.html'html = open(filename).read()tree = lxml.etree.fromstring(html)table = tree.xpath('//table')[0]

# ... chato chato chato ...# tentativa# erro# erro# erro

Page 13: [PyBr11] Lendo dados tabulares: versão pythônica

...

Page 14: [PyBr11] Lendo dados tabulares: versão pythônica

XLS

Page 15: [PyBr11] Lendo dados tabulares: versão pythônica

#comofaz? #xlspip install xlrd

import xlrd

filename = 'tesouro-direto.xls'book = xlrd.open_workbook(filename, formatting_info=True)sheet = book.sheet_by_index(0)NUMERO_DE_COLUNAS = 7 # WTF?header = [sheet.cell(0, col).value for col in range(NUMERO_DE_COLUNASNUMERO_DE_LINHAS = 19 # WTF?data = [[sheet.cell(row, col).value for col in range(NUMERO_DE_COLUNAS for row in range(1, NUMERO_DE_LINHAS + 1)]

for row_data in data: row = dict(zip(header, row_data)) print row # TODO: fazer conversor de datetime # TODO: fazer conversor de date # TODO: fazer conversor de decimal # TODO: fazer conversor de percent

Page 16: [PyBr11] Lendo dados tabulares: versão pythônica

Resultado...{u'preco_compra': 0.0, u'timestamp': 42314.73819444444, u'vencimento'

Page 17: [PyBr11] Lendo dados tabulares: versão pythônica

..., u'timestamp': 42314.73819444444, ...

Page 18: [PyBr11] Lendo dados tabulares: versão pythônica

ProblemasAPIs diferentesEntender o formato (mas eu só quero os dados...)Aprender a usar a bibliotecaConverter os dados (acha que a biblioteca vai converter?)(isso é só pra ler...)Testar os conversores (e o tempo?)

Page 19: [PyBr11] Lendo dados tabulares: versão pythônica

rows to therescue

pip install rows

apt-get install rows

dnf install rows

github.com/turicas/rows

Page 20: [PyBr11] Lendo dados tabulares: versão pythônica

#comofaz? #csv #dojeitocertoimport rows

table1 = rows.import_from_csv('tesouro-direto.csv')

for row in table1: print row

Page 21: [PyBr11] Lendo dados tabulares: versão pythônica

#comofaz? #xls #dojeitocertoimport rows

table2 = rows.import_from_xls('tesouro-direto.xls')

for row in table2: print row

Page 22: [PyBr11] Lendo dados tabulares: versão pythônica

#comofaz? #html#dojeitocerto

import rows

table3 = rows.import_from_html('tesouro-direto.html')

for row in table3: print row

Page 23: [PyBr11] Lendo dados tabulares: versão pythônica

assert list(table1) == list(table2) == list(table3)

Page 24: [PyBr11] Lendo dados tabulares: versão pythônica

print table1[0]

Row(timestamp=datetime.datetime(2015, 11, 6, 17, 43), titulo=u'Tesouro IPCA+ com Juros Semestrais 2017 (NTNB)', vencimento=datetime.date(2017, 5, 15), taxa_compra=Decimal('0.0702'), taxa_venda=Decimal('0.063'), preco_compra=0.0, preco_venda=2792.97)

# namedtuple #FTW \o/

Page 25: [PyBr11] Lendo dados tabulares: versão pythônica
Page 26: [PyBr11] Lendo dados tabulares: versão pythônica

FuncionalidadesAPI única (o formato não importa)Leitura e escritaCSV, HTML, XLS, XLSX, JSON, TXT, SQLite...Conversão automática de dadosTem uma command-line interface! \o/Eu já fiz os testes

Page 27: [PyBr11] Lendo dados tabulares: versão pythônica

#meajudaPython 3Outros plugins (PDF, OFX, ODS, DBF...)Decisões de arquiteturaSprint na quarta? o/

Page 28: [PyBr11] Lendo dados tabulares: versão pythônica

Obrigado! (:Turicas aka Álvaro Justen

{ , , ,}/turicas

twitter.com github.com youtube.comslideshare.net

[email protected]