encoding/csv

3140de70ab04d8f9fc979447b0b4fb16?s=47 pinzolo
April 29, 2017

 encoding/csv

そうだ Go、京都。でのLTスライド

3140de70ab04d8f9fc979447b0b4fb16?s=128

pinzolo

April 29, 2017
Tweet

Transcript

  1. encoding/csv ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)

  2. $ whoami » NODA Masato » @pinzolo » גࣜձۭࣾ »

    Goྺ1೥ະຬ » goroutine ࢖͍͜ͳ͍ͤͯͳ͍੎ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)
  3. Go Ͱ࡞ͬͨ΍ͭ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)

  4. tablarian ίϚϯυϥΠϯͰςʔϒϧఆٛҾ͘Ϡπ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)

  5. csvutil CSVʹରͯ͋͠Ε͜ΕͰ͖ΔϠπ $ csvutil generate --size 5 --count 10 --header

    ࢯ໊:༣ศ൪߸:ॅॴ:ݐ෺:ϝʔϧ | \ csvutil name --name ࢯ໊ | \ csvutil address --zip-code ༣ศ൪߸ --prefecture ॅॴ --city ॅॴ --town ॅॴ --block-number | \ csvutil building --column ݐ෺ | \ csvutil email --column ϝʔϧ ࢯ໊,༣ศ൪߸,ॅॴ,ݐ෺,ϝʔϧ ౔ా ܚଠ࿠,061-8035,ژ౎෎࿨ޫࢢ੢य़ผӺલ੢ொ42-18,ϓϨεςʔδલՏݪ1926,tempora_doloribus_inventore@twitterbeat.info ݹ઒ ಞਓ,468-2410,௕࡚ݝ౦ࠜࢢޚଂౡଜҰԁ16,ςϥεϥΠϑ1421,charlesstevens@eire.mil ԣా ඣѮ,197-3356,Ѫ஌ݝࢤ໦ࢢਆాࠤٱؒՏ؛44-12-6,ιϨΠϢຑੜ1229,qjordan@roomm.edu Ԯ໺ ٢จ,819-3023,౦ژ౎େࡕࢢฏ໺ُ۠ඌொ44-24-2,άϦʔϯΤεςʔτ1727,tempore_numquam_consequatur@midel.org ৿ࢁ ࠫߐ,289-9318,Ԭࢁݝେࡕࢢ౦ཎ઒۠ࡩ࡞42-6,άϨʔεϰΟϨοδӉ࿨ொ໩ॴ1118,ucooper@voonder.net େ໺ ࠫف,093-0401,৽ׁݝՏ๺܊಺ṗொ໦ڵொ46-10,ύʔϧϨδσϯεསέݪ806,victorpierce@dynabox.edu ࢁ࡚ ྰࡊ,065-9621,ฌݿݝେࡕࢢ࿘଎۠ീ഼ொ39-18,εΠʔτίʔτޫொ506,qui_aut_nihil@meeveo.biz ۚ୔ ༏ೋ,201-6394,ಙౡݝؠख܊࣢ੴொ೔ஔ໺ా36-10,ϓϨεςʔδീ৿Ṥ௕421,ibell@demizz.com দݪ ੣ޗ,877-1329,ੴ઒ݝ٢໺܊໺ഭ઒ଜṗ38-5-10,Ψʔσϯλϫʔ౻Տ಺1120,ab_cum@voomm.info ࡔҪ ྗ໵,967-7366,Ѫඤݝେࡕࢢ੢۠നௗொத௡԰41,ϨΠΫύʔΫ609,perferendis_earum@quamba.edu ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)
  6. GoͰ͸ encoding/csv ύοέʔδͱ͍͏ CSV Λѻ͏ඇৗʹγϯϓϧͳ ύοέʔδ͕͋Γ·͢ɻ csvutil Λ࡞Δͱ͖ʹ͍Ζ͍Ζ৮ͬͨͷΛڞ༗͠·͢ɻ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)

  7. ओʹ࢖͏΍ͭ csv.Reader io.Reader Λ౉ͯ͠ Read ͔ ReadAll ͢Δ͚ͩɻ csv.Writer io.Writer

    Λ౉ͯ͠ Write -> Flush ͔ WriteAll ͢Δ͚ͩɻ ͲͪΒ΋ io.Reader io.Writer ͱޓ׵ੑ͸ͳ͘ɺ·ͨ interface Ͱ΋͋ Γ·ͤΜɻ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)
  8. αϯϓϧίʔυ src, _ := os.Open("src.csv") defer src.Close() dst, _ :=

    os.Create("dst.csv") defer dst.Close() r := csv.NewReader(src) w := csv.NewWriter(dst) for { record, err := r.Read() if err != nil { if err == io.EOF { break } panic(err) } w.Write(record) } ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)
  9. ϑΥʔϚοτ encoding/csv ύοέʔδ͸ RFC4180ʹ४ڌͨ͠ϑΥʔϚοτΛαϙʔ τ͍ͯ͠·͢ɻ ͨͩ͠σϑΥϧτͷվߦจࣈ͕ LF ͩͬͨΓɺεϖʔε͚ͩͷཁૉΛμϒ ϧΫΦʔτͰғΜͩΓ͠·͢ɻ ͦ͏ͩ

    Goɺژ౎ɻ(2017-04-29)
  10. จࣈίʔυ csv.Reader ΋ csv.Writer ΋σϑΥϧτͰ͸จࣈྻΛUTF-8ͱͯ͠ѻ͍ ·͢ɻ ͨͩ͠BOMʹ͸ରԠ͍ͯ͠·ͤΜɻ BOMΛѻ͏ʹ͸Ұ޻෉ඞཁͰ͢ɻ1 ͓͔͛Ͱ CSV

    ͱ͍͑͹ Excel Ͱ͕ܾͯ͢͠૬ੑ͸Α͋͘Γ·ͤΜɻ 1 GoͰBOM෇͖ͷUTF8ͳCSVΛѻ͏ʹ͸ | tail -f pinzo.log ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)
  11. ίϝϯτ csv.Reader ʹ͸ Comment ϓϩύςΟ͕͋Γɺ͜ͷ஋͔Β࢝·ΔߦΛί ϝϯτߦͱͯ͠εΩοϓ͠·͢ɻ ͨͩ͠ rune ͳͷͰ //

    Ͱ࢝·ΔߦΛίϝϯτߦͱΈͳ͢Α͏ͳ͜ͱ͸Ͱ ͖·ͤΜɻ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)
  12. ΫΦʔτ ಛఆͷཁૉΛΫΦʔτ෇͖Ͱग़ྗ͢Δ͜ͱ͸Ͱ͖·ͤΜɻ͢΂ͯ csv.Writer ͕ػցతʹ෇༩͠·͢ɻ ·ͨ csv.Reader ͰಡΈࠐΜͩཁૉ͕ΫΦʔτ෇͖͔Ͳ͏͔΋Θ͔Γ· ͤΜɻ ΫΦʔτ͕ෆἧ͍ͳཁૉ͕͋Ε͹ΤϥʔʹͳΓ·͕͢ɺLazyQuote Λ

    true ʹ͢Δͱڐ༰͠·͢ɻ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)
  13. σϦϛλ σϦϛλΛઃఆ͢Δ͜ͱ͸Ͱ͖·͢ɻ ; ʹͨ͠Γλϒจࣈʹ͢Δ͜ͱ͕ Ͱ͖·͢ɻ ͨͩ Comma ͬͯϓϩύςΟ໊͸Ͳ͏ͳͷʁʁͱ͸ࢥ͍·͕͢ɻ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)

  14. ཁૉ਺ FieldsPerRecord ͕ 0 ͳΒ࠷ॳͷߦͷཁૉ਺͕ɺਖ਼਺ͳΒࢦఆ͞Εͨ਺ Λཁૉ਺ͱͯ͠ѻ͍ɺաෆ଍͕͋ΔߦΛಡΈࠐΉͱΤϥʔʹͳΓ·͢ɻ ͜ͷϓϩύςΟʹෛ਺Λࢦఆ͢ΔͱɺνΣοΫΛߦΘͳ͍ͷͰɺཁૉ਺ ͕ݻఆͰͳ͍৔߹͸ෛ਺Λઃఆ͠·͢ɻ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)

  15. ͱɺ ͍͏Θ͚ Ͱɻ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)

  16. ͪΐͬͱ༥௨͕͖͔ͳ͍ ͚ΕͲɺGo Ͱ CSV ͸؆ ୯ʹѻ͑·͢ɻ ͦ͏ͩ Goɺژ౎ɻ(2017-04-29)