Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests some unicode CSV solutions available for Python.
  4. """
  5.  
  6. from __future__ import unicode_literals
  7.  
  8. import sys
  9.  
  10. TESTS = {
  11. 'German': [u'Straße', u'auslösen', u'zerstören'],
  12. 'French': [u'français', u'américaine', u'épais'],
  13. 'Chinese': [u'中國的', u'英語', u'美國人']
  14. }
  15.  
  16.  
  17. def perform_test_python2(csv):
  18. from io import BytesIO
  19.  
  20. buffer = BytesIO()
  21. writer = csv.writer(buffer, encoding='UTF-8')
  22. writer.writerows([TESTS.keys()])
  23. for i_row in zip(*TESTS.values()):
  24. writer.writerows([i_row])
  25.  
  26.  
  27. def perform_test_python3(csv):
  28. from io import StringIO
  29.  
  30. buffer = StringIO()
  31. writer = csv.writer(buffer)
  32. writer.writerows([TESTS.keys()])
  33. for i_row in zip(*TESTS.values()):
  34. writer.writerows([i_row])
  35.  
  36. def perform_test(csv):
  37. try:
  38. sys.stdout.write(' Python 2 syntax: ')
  39. perform_test_python2(csv)
  40. except Exception as e:
  41. print(" ERROR")
  42. else:
  43. print(" OK")
  44.  
  45. try:
  46. sys.stdout.write(' Python 3 syntax: ')
  47. perform_test_python3(csv)
  48. except Exception as e:
  49. print(" ERROR")
  50. else:
  51. print(" OK")
  52.  
  53. print("Using csv")
  54. import csv
  55. perform_test(csv)
  56.  
  57. print("Using unicodecsv")
  58. import unicodecsv
  59. perform_test(unicodecsv)
  60.  
  61. print("Using csv342")
  62. import csv342
  63. perform_test(csv342)
  64.  
  65. print("Using csv23")
  66. import csv23
  67. perform_test(csv23)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement