tparsecsv.nim 929 B

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