You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

csv-comparator.py 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #######################################################################
  2. # Software License Agreement (AGPL-3 License)
  3. #
  4. # OpenViBE SDK Test Software
  5. # Based on OpenViBE V1.1.0, Copyright (C) Inria, 2006-2015
  6. # Copyright (C) Inria, 2015-2017,V1.0
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License version 3,
  10. # as published by the Free Software Foundation.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program.
  19. # If not, see <http://www.gnu.org/licenses/>.
  20. #######################################################################
  21. import csv
  22. import sys
  23. # test script parameter arg1: reference file csv
  24. # test script parameter arg2: test file csv
  25. # assign arguments to variable
  26. if len(sys.argv) != 3 :
  27. print('incorrect args')
  28. sys.exit(101)
  29. referenceData=sys.argv[1]
  30. testData=sys.argv[2]
  31. def compareCells (file1,file2):
  32. """Compare values of cell from two list of lists"""
  33. # select the ligne for both csv file
  34. for line_index, (input_line, output_line) in enumerate(zip(file1, file2)) :
  35. #check line sizes
  36. if len(input_line) != len(output_line):
  37. print('input and output cells size are different on line %s' % (line_index + 2))
  38. print('input cells: %s' % len(input_line))
  39. print('output cells: %s' % len(output_line))
  40. sys.exit(107)
  41. for col_index, (inputCell, outputCell) in enumerate(zip(input_line, output_line)):
  42. if inputCell != "" and abs(float(outputCell)-float(inputCell)) > sys.float_info.epsilon:
  43. print('error on line %s and column %s' % (line_index + 2, col_index+1))
  44. print( 'the input cell %s is different from output cell %s' % (inputCell, outputCell))
  45. sys.exit(107)
  46. try:
  47. #open files associated with the variables
  48. with open(referenceData, 'r') as fileReferenceData :
  49. readerReferenceData = csv.reader(fileReferenceData, delimiter=';')
  50. inputData = [row for row in readerReferenceData]
  51. with open(testData, 'r') as fileTestData :
  52. readerTestData = csv.reader(fileTestData, delimiter=';')
  53. outputData = [row for row in readerTestData]
  54. except IOError as err:
  55. print('missing input or ouput file')
  56. print('missing file: %s'%(err.filename))
  57. sys.exit(106)
  58. #check the file size of input and output files
  59. if len(inputData) != len(outputData) :
  60. print('Input (%s lines) and output (%s lines) files size are different :' % (len(inputData), len(outputData)))
  61. sys.exit(107)
  62. # remove the headers and compare them
  63. if inputData.pop(0) != outputData.pop(0) :
  64. print('Header is different')
  65. sys.exit(108)
  66. #perform the cell comparison
  67. compareCells(inputData, outputData)
  68. print('All values are identiqual')
  69. sys.exit()