of 49 /49
Dados Tabulares: A Maneira Pythônica Turicas aka Álvaro Justen VIII Encontro PythOnRio 30 de abril de 2016, Elo Group - Rio, Brasil

Dados tabulares: A Maneira Pythônica

Embed Size (px)

Text of Dados tabulares: A Maneira Pythônica

  1. 1. Dados Tabulares: A Maneira Pythnica Turicas aka lvaro Justen VIII Encontro PythOnRio 30 de abril de 2016, Elo Group - Rio, Brasil
  2. 2. Turicas, prazer! =) { , , , }/turicas twitter.com github.com youtube.com slideshare.net [email protected] turicas.info
  3. 3. Quem sou eu? No tenho graduao No trabalho em empresa
  4. 4. Software Livre
  5. 5. Python
  6. 6. Arduino
  7. 7. CursoDeArduino.com.br
  8. 8. pythonquito.tk
  9. 9. E vocs?Quem jsabe Python? Quem trabalhacom Python?
  10. 10. bit.ly/pyrio-rows
  11. 11. Jornalismo de Dados1. Captura: buscar e baixar 2. Normalizao: converter aum formato melhor (ex: HTML paraCSV) 3. Anlise: ver o que te serve 4. Visualizao: mostrar pro mundo!
  12. 12. Jornalismo de Dados [2]4. Visualizao: cerejado bolo 3. Anlise: desa ador 2. Normalizao: chato pracaralho 1. Captura: chato, de vez em quando desa ador
  13. 13. Captura + Normalizao
  14. 14. Normalizaopipuninstallbeaufitulsoup4 pipuninstalllxml pipinstallrows
  15. 15. CSV
  16. 16. csv em Pythonimportcsv reader=csv.DictReader(open('tesouro-direto.csv')) #TODO:identificardialectoCSV forrowinreader: printrow #todoesstring=/ #TODO:crearconversindedatetime #TODO:crearconversindedate #TODO:crearconversindecimal #TODO:crearconversinpercent
  17. 17. HTML
  18. 18. HTML
  19. 19. A vida no to fcil! :D
  20. 20. html em Python + lxmlpipinstalllxml importlxml.etree filename='tesouro-direto.html' html=open(filename).read() tree=lxml.etree.fromstring(html) table=tree.xpath('//table')[0] #...aburridoaburridoaburrido... #tentativa #error #tentativa #error #error #error
  21. 21. :-/ ...
  22. 22. XLS
  23. 23. xls em Python + xlrd pipinstallxlrd importxlrd 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).valueforcolinrange(NUMERO_DE_COLUNAS NUMERO_DE_LINEAS=19#WTF? data=[[sheet.cell(row,col).valueforcolinrange(NUMERO_DE_COLUNAS forrowinrange(1,NUMERO_DE_LINEAS+1)] forrow_dataindata: row=dict(zip(header,row_data)) printrow #TODO:crearconversindedateydatetime #TODO:crearconversindedecimal #TODO:crearconversindepercent
  24. 24. Resultado...{u'preco_compra':0.0,u'timestamp':42314.73819444444,u'vencimento'
  25. 25. Que porra essa? ...,u'timestamp':42314.73819444444,...
  26. 26. Por que csv, lxml, xlrd etc. so ruins?Aprender sobre o formato (mesmo que voc s queiraos dados) Aprender umabibliotecapor formato (APIs diferentes) Converter os dados (nem sempre as bibliotecas convertem) Testar as funes de converso (voc usaTDD, certo?) Exemplos s de leitura(escritapode ser pior!)
  27. 27. rows to the rescue! pip install rows # Python Package Index apt-get install rows # Debian! dnf install rows # Fedora github.com/turicas/rows
  28. 28. csv com Python + rows importrows table1=rows.import_from_csv('tesouro-direto.csv') forrowintable1: printrow
  29. 29. Row(timestamp=datetime.datetime(2015,11,6,17,43),titulo=u'Tesouro Row(timestamp=datetime.datetime(2015,11,6,17,43),titulo=u'Tesouro ...
  30. 30. xls com Python + rows importrows table2=rows.import_from_xls('tesouro-direto.xls') forrowintable2: printrow
  31. 31. Row(timestamp=datetime.datetime(2015,11,6,17,43),titulo=u'Tesouro Row(timestamp=datetime.datetime(2015,11,6,17,43),titulo=u'Tesouro ...
  32. 32. html com Python + rows importrows table3=rows.import_from_html('tesouro-direto.html') forrowintable3: printrow
  33. 33. Row(timestamp=datetime.datetime(2015,11,6,17,43),titulo=u'Tesouro Row(timestamp=datetime.datetime(2015,11,6,17,43),titulo=u'Tesouro ...
  34. 34. assertlist(table1)==list(table2)==list(table3)
  35. 35. printtable1[0] Row(timestamp=datetime.datetime(2015,11,6,17,43), titulo=u'TesouroIPCA+comJurosSemestrais2017(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#FTWo/
  36. 36. Funcionalidades API nica(no importao formato) Escrever to fcilquanto ler (pythonic way) CSV, HTML, XLS, XLSX, JSON, TXT, SQLite... Converso automtica de dados Command-line interface!o/ Euj z os testes
  37. 37. Live codingBiblioteca CLI
  38. 38. #ajudalucianoPython 3 (WIP) Site/documentao (Sphinx) rows as aservice (demo site)
  39. 39. #ajudaluciano [2] Outros Plugins PDF(leitura) ODS (escrita) DBF MongoDB PostgreSQL (WIP) MariaDB(WIP) OFX (WIP)
  40. 40. That's it!
  41. 41. pythonnordeste.org 9 a11 de junho Teresina/PI
  42. 42. sl.org.br 13 a16 de julho Porto Alegre/RS
  43. 43. pythonsudeste.org 2 e 3 de setembro Belo Horizonte/MG
  44. 44. pythonbrasil.org.br 13 a18 de outubro Florianpolis/SC
  45. 45. Curso de Python Online e Gratuito! bit.ly/pyonline
  46. 46. ?
  47. 47. Obrigado! (: Turicas aka lvaro Justen { , , , }/turicas twitter.com github.com youtube.com slideshare.net [email protected] turicas.info bit.ly/pyrio-rows