Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

73 строки
2.6 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. import webnotes
  24. def read_csv_content_from_uploaded_file():
  25. from webnotes.utils.file_manager import get_uploaded_content
  26. fname, fcontent = get_uploaded_content()
  27. return read_csv_content(fcontent)
  28. def read_csv_content_from_attached_file(doc):
  29. if not doc.file_list:
  30. msgprint("File not attached!")
  31. raise Exception
  32. try:
  33. from webnotes.utils.file_manager import get_file
  34. fid = doc.file_list.split(",")[1]
  35. fname, fcontent = get_file(fid)
  36. return read_csv_content(fcontent)
  37. except Exception, e:
  38. webnotes.msgprint("""Unable to open attached file. Please try again.""")
  39. raise Exception
  40. def read_csv_content(fcontent):
  41. import csv
  42. from webnotes.utils import cstr
  43. rows = []
  44. try:
  45. reader = csv.reader(fcontent.splitlines())
  46. # decode everything
  47. csvrows = [[val for val in row] for row in reader]
  48. for row in csvrows:
  49. newrow = []
  50. for val in row:
  51. if webnotes.form_dict.get('ignore_encoding_errors'):
  52. newrow.append(cstr(val.strip()))
  53. else:
  54. try:
  55. newrow.append(unicode(val.strip(), 'utf-8'))
  56. except UnicodeDecodeError, e:
  57. raise Exception, """Some character(s) in row #%s, column #%s are
  58. not readable by utf-8. Ignoring them. If you are importing a non
  59. english language, please make sure your file is saved in the 'utf-8'
  60. encoding.""" % (csvrows.index(row)+1, row.index(val)+1)
  61. rows.append(newrow)
  62. return rows
  63. except Exception, e:
  64. webnotes.msgprint("Not a valid Comma Separated Value (CSV File)")
  65. raise e