python3 python3:(unicode error) 'utf-8' codec can't decode

一、问题

[Sun Oct 02 16:37:55 2016] [error] [client 127.0.0.1]   File "D:/software/Apache_HTTP_Server2.2/cgi-bin/hello_test_get.py", line 23\r
[Sun Oct 02 16:37:55 2016] [error] [client 127.0.0.1] SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xb2 in position 0: invalid start byte\r
在Apache http sever 中用CGI编程  一直报以上错误

py代码如下:

#! D:/software/anaconda3/python.exe
#-*- coding:UTF-8 -*-

#Created on 2016年10月2日

#@author: c

# CGI处理模块
import cgi, cgitb 

# 创建 FieldStorage 的实例化
form = cgi.FieldStorage() 

# 获取数据
site_name = form.getvalue('name')
site_url  = form.getvalue('url')

print ("Content-type:text/html")
print ()
print ("<html>")
print ("<head>")
print ("<meta charset='gb2312'>")
print ("<title>菜鸟教程 CGI 测试实例</title>")
print ("</head>")
print ("<body>")
print ("<h2>%s官网:%s</h2>" % (site_name, site_url))
print ("</body>")
print ("</html>")
以上代码在Pydev中跑没有问题


基本知识:在python中默认的编码格式是 utf-8。所以怎么会报不能按 utf-8来解码嘞?一头雾水啊。


问题的解决:

使用notepad++打开

hello_test_get.py
发现文件存储的格式是ANSI


只要将保存文件的格式换成UTF-8就好了

只用notepad++打开test.py >> 菜单栏Encoding(编码)>> Convert to UTF-8(转化成utf-8)


在运行test.py问题解决



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值