我正在使用Python-2.6 CGI脚本,但在服务器日志中发现此错误json.dumps(),
Traceback (most recent call last):
File "/etc/mongodb/server/cgi-bin/getstats.py", line 135, in <module>
print json.dumps(??__get?data())
File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa5 in position 0: invalid start byte
Run Code Online (Sandbox Code Playgroud)
在这里,
?__get?data()功能返回dictionary {}.
在发布这个问题之前,我已经提到了这个问题.
以下行是伤害JSON编码器,
now = datetime.datetime.now()
now = datetime.datetime.strftime(now, '%Y-%m-%dT%H:%M:%S.%fZ')
print json.dumps({'current_time': now}) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 utf-8 编码将数据写入 Excel 工作表。现在我收到以下错误并进行了完整的回溯 -->
Traceback (most recent call last):
File "C:\Users\varun\Desktop\Python_testfiles\Reports Automation\Txn.py", line 142, in <module>
domesticsheet.write(row, j, txn[payuid][j])
File "C:\Python27\lib\site-packages\xlwt\Worksheet.py", line 1030, in write
self.row(r).write(c, label, style)
File "C:\Python27\lib\site-packages\xlwt\Row.py", line 240, in write
StrCell(self.__idx, col, style_index, self.__parent_wb.add_str(label))
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 326, in add_str
return self.__sst.add_str(s)
File "C:\Python27\lib\site-packages\xlwt\BIFFRecords.py", line 24, in add_str
s = unicode(s, self.encoding)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 11: invalid start byte
Run Code Online (Sandbox Code Playgroud)
主要问题是我随机收到此错误。我运行了前几天对应的数据代码,运行得很好。我尝试使用“utf-16”和“ascii”编码而不是utf-8,但错误仍然存在(尽管错误声明已更改。)
有什么方法可以消除这个错误吗?另外,我想知道为什么会出现这个错误(我是Python的初学者)。任何帮助将不胜感激。是否有必要提供某种编码类型?
如果需要看代码如下-->
filehandler[booknumber] = xlwt.Workbook(encoding = "utf-8")
domesticsheet …Run Code Online (Sandbox Code Playgroud)