我正在編寫單元測試用例。我創建了一個文件對象和閱讀它,但我收到以下錯誤:當讀取文件時<<:'str'和'int'不受支持的操作數類型
unsupported operand type(s) for <<: 'str' and 'int'
方法:
def uploadExamineeDetails(request, exam_id):
try:
upload_file = request.FILES['upload-file']
except Exception:
return [_('Uploaded file is required.')]
try:
exam = get_object_or_404_from_admin(CourseExam, request, exam_id)
book = xlrd.open_workbook(file_contents=upload_file.read())
# further code
我的測試代碼:在Excel文件
def test_uploadExamineeDetails(self):
self.examinee_result = os.path.join(os.path.dirname(settings.BASE_DIR),\
'var/sample_files_for_testing/examinees_result_upload.xls')
file = File(open(self.file, errors='ignore'))
uploaded_file = InMemoryUploadedFile(file=file, field_name='upload-file', name='examinee_result.xls',
content_type = 'application/vnd.ms-excel', size = file.size, charset = None)
self.request.FILES['upload-file'] = uploaded_file
xlrd.open_workbook(file_contents=uploaded_file.read())
response = uploadExamineeDetails(self.request, 1)
assert isinstance(response, tuple), 'should upload the examinee details'
數據:
[{'Enrollment Number': '', 'Username': 'exam_course_manager',
'row_num': 1, 'Obtained Score': 60.0, 'GR Number': ''},
{'Enrollment Number': '', 'Username': 'instructor',
'row_num': 2, 'Obtained Score': 20.0, 'GR Number': ''}]
回溯日期:
(py_3.5_dj_1.9) [email protected]:~/Projects/DROANA_3.0/droana/droana$ py.test droana/apps/course_planner/tests/test_methods.py
============================= test session starts ==============================
platform linux -- Python 3.5.2, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
Django settings: droana.test_settings (from command line option)
rootdir: /home/dikshaj/Projects/DROANA_3.0/droana/droana, inifile: pytest.ini
plugins: django-3.1.2, cov-2.4.0, ipdb-0.1.dev2
collected 1 items
droana/apps/course_planner/tests/test_methods.py F
----------- coverage: platform linux, python 3.5.2-final-0 -----------
Coverage HTML written to dir htmlcov
=================================== FAILURES ===================================
____________________ TestMethods.test_uploadExamineeDetails ____________________
self = <droana.apps.course_planner.tests.test_methods.TestMethods testMethod=test_uploadExamineeDetails>
def test_uploadExamineeDetails(self):
"""
Test uploadExamineeDetails method
"""
self.examinee_result = os.path.join(os.path.dirname(settings.BASE_DIR),\
'var/sample_files_for_testing/examinees_result_upload.xls')
file = File(open(self.file, errors='ignore'))
uploaded_file = InMemoryUploadedFile(file=file, field_name='upload-file', name='examinee_result.xls',
content_type = 'application/vnd.ms-excel', size = file.size, charset = None)
self.request.FILES['upload-file'] = uploaded_file
> xlrd.open_workbook(file_contents=uploaded_file.read())
droana/apps/course_planner/tests/test_methods.py:100:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/opt/myvirtual/py_3.5_dj_1.9/lib/python3.5/site-packages/xlrd/__init__.py:435: in open_workbook
ragged_rows=ragged_rows,
/opt/myvirtual/py_3.5_dj_1.9/lib/python3.5/site-packages/xlrd/book.py:91: in open_workbook_xls
biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
/opt/myvirtual/py_3.5_dj_1.9/lib/python3.5/site-packages/xlrd/book.py:1226: in getbof
opcode = self.get2bytes()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <xlrd.book.Book object at 0x7fb57b458c88>
def get2bytes(self):
pos = self._position
buff_two = self.mem[pos:pos+2]
lenbuff = len(buff_two)
self._position += lenbuff
if lenbuff < 2:
return MY_EOF
lo, hi = buff_two
> return (BYTES_ORD(hi) << 8) | BYTES_ORD(lo)
E TypeError: unsupported operand type(s) for <<: 'str' and 'int'
/opt/myvirtual/py_3.5_dj_1.9/lib/python3.5/site-packages/xlrd/book.py:631: TypeError
的代碼讀取此行拋出一個異常:
xlrd.open_workbook(file_contents=upload_file.read())
這是我做了什麼。我創建一個文件,將其存儲在目錄中,然後打開它並創建一個內存對象,如代碼所示。根據錯誤我知道當你試圖比較一個字符串到int時發生這個錯誤。但我不明白爲什麼這發生在閱讀文件,以及在哪裏。
有誰知道問題是什麼?
你可以在你的問題中包含你看到的堆棧跟蹤嗎?對於它的價值,你顯示的錯誤信息涉及位移運算符(<<),我在你的任何代碼中都沒有看到,所以我猜你正在使用的一些庫代碼是拋出錯誤。堆棧跟蹤將有助於縮小可能導致問題的參數。 –
我已經添加了回溯,即使我上傳的txt文件仍然得到相同的問題。我認爲讀取文件時發生問題。如果有人能給我解決這個問題,我將非常感激。 –
它看起來像xlrd的問題,但我無法在網上找到解決方案,我不熟悉該庫。如果將'xlrd.open_workbook(file_contents = upload_file.read())'行更改爲'xlrd.open_workbook(filename ='name-of-file-on-disk.xls')'',那麼您已經保存了文件內容添加到實際文件中,問題是否仍然存在? –