tparsecsv.nim 880 B

123456789101112131415161718192021222324252627282930313233
  1. include parsecsv
  2. import strutils, os
  3. import std/assertions
  4. block: # Tests for reading the header row
  5. let content = "\nOne,Two,Three,Four\n1,2,3,4\n10,20,30,40,\n100,200,300,400\n"
  6. writeFile("temp.csv", content)
  7. var p: CsvParser
  8. p.open("temp.csv")
  9. p.readHeaderRow()
  10. while p.readRow():
  11. let zeros = repeat('0', p.currRow-2)
  12. doAssert p.rowEntry("One") == "1" & zeros
  13. doAssert p.rowEntry("Two") == "2" & zeros
  14. doAssert p.rowEntry("Three") == "3" & zeros
  15. doAssert p.rowEntry("Four") == "4" & zeros
  16. p.close()
  17. when not defined(testing):
  18. var parser: CsvParser
  19. parser.open("temp.csv")
  20. parser.readHeaderRow()
  21. while parser.readRow():
  22. echo "new row: "
  23. for col in items(parser.headers):
  24. echo "##", col, ":", parser.rowEntry(col), "##"
  25. parser.close()
  26. removeFile("temp.csv")
  27. # Tidy up
  28. removeFile("temp.csv")