有时候正常没有问题的程序会报错,可能跟注释里面的中文也有关系

 with open('photo1.jpg','rb') as file:
data = file.read()
#print(data)
with open('photo5.jpg','wb') as newfile:
newfile.write(data)

这个代码是没有问题的

 with open('photo1.jpg','rb') as file:
data = file.read()
#print(data)
with open('photo2.jpg','wb') as newfile: # 以“wb”模式写入
newfile.write(data)

这样就会报错了

 # coding=gbk
with open('photo1.jpg','rb') as file:
data = file.read()
#print(data)
with open('photo10jpg','wb') as newfile: # 以“wb”模式写入
newfile.write(data)

这样就又好了

程序中出现中文,运行的时候出现如下错误:

SyntaxError: Non-UTF-8 code starting with 'xc1' in file C:...xxx.py on line 8, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

导致出错的根源就是编码问题。
解决方案是:
在程序最上面加上:

# coding=gbk

这样程序就可以正常运行了。

玄学bug(1)---注释里面的中文会报错的更多相关文章

  1. PyCharm注释中出现中文运行报错的解决办法

    SyntaxError: Non-UTF-8 code starting with '..... 方法一:在文件首行加上 # -*- coding:utf-8 -*- 方法二:更改编码格式 File ...

  2. webstorm中sass编译时目录或内容包含中文字符报错

    ruby版本:ruby 2.3.1p112 (2016-04-26 revision 54768) [x64-mingw32] sass版本:Sass 3.4.22 (Selective Steve) ...

  3. 记一次 Hibernate 插入数据中文乱码报错解决

    错误描述 程序运行,向表中插入数据(包含中文)报错:\xE6\xB2\x88\xE9\x9B\xAA... 但是自己另外新建一个数据库手动插入数据中文正常,同样修改数据库,表的编码之后同样不行.而且 ...

  4. 提高编译速度! 第一次运行需要注释掉,不然会报错,因为需要编译SO库文件 !

    // 提高编译速度! 第一次运行需要注释掉,不然会报错,因为需要编译SO库文件 ! tasks.whenTaskAdded { task -> if (task.name.contains(&q ...

  5. sass注释中有中文出现报错解决方法

    在使用koala编译sass成css过程中出现这样的报错 后来查资料说是自己在sass中的注释中有中文引起的, 解决方案: 进入C:\Ruby25-x64\lib\ruby\gems\2.5.0\ge ...

  6. Control character in cookie value, consider BASE64 encoding your value , java操作cookie遇到中文会报错的解决方案

    项目当中用到cookie保存中文,但是会报如下错误: Control character in cookie value, consider BASE64 encoding your value 大概 ...

  7. Cookie保存中文用户名报错(500)

    在用Cookie保存用户名时候,当用户名是中文的时候服务器报错了. HTTP Status 500 - An exception occurred processing JSP page /dolog ...

  8. opencv读取中文路径报错的问题

    ) ## 经验证,不需要再转bgr,myImread的读图结果已经是和imread一样的 return img

  9. python中time.strftime不支持中文,报错UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: encoding error

    使用time.strftime将 "2020-10-10 10:10:10" 转化为  2020年10月10日10时10分10 报错: import time timestr=&q ...

随机推荐

  1. vuejs通过filterBy,orderBy实现搜索筛选,降序排序数据实例

    直接贴代码了: 先上输入前的样子: <style> #example{margin:100px auto;width:600px;} .show{margin:10px;} #search ...

  2. [转]原生JS-查找相邻的元素-siblings方法的实现

    在针对element的操作里,查找附近的元素是一个不可少的过程,比如在实现tab时,其中的一个div增加了“on”class,其他的去除“on”class.如果用jquery的朋友就肯定不会陌生sib ...

  3. python--文件流读写

    在讲述fileinput模块之前,首先说一下python内置的文件API—open()函数以及与其相关的函数. 我这里主要讲讲其中四个比较重要和常用的方法,更多的方法,可以参考:菜鸟教程http:// ...

  4. 章节二、1-java概述-数据类型

    一.数据类型 1.基本数据类型 a.数值型 1.整数:byte(1个字节=8位) min:-128 max:127 default:0 .short(2个字节=16位) min:-32768 max: ...

  5. [20190214]11g Query Result Cache RC Latches补充.txt

    [20190214]11g Query Result Cache RC Latches补充.txt --//上午测试链接:http://blog.itpub.net/267265/viewspace- ...

  6. Sql Server 按格式输出日期

    SELECT dbo.fn_Data(getdate(),'yyyymmdd') CREATE FUNCTION [dbo].[fn_Data] (@date as datetime, @format ...

  7. 【底层原理】深入理解Cache (下)

    得到了我的PC的cache参数如下: L1 Cache : 32KB , 8路组相连,linesize为 64Byte 64个组 L2 Cache:256KB 8路组相连,linesize为 64By ...

  8. c/c++ 数组和指针

    c/c++ 数组和指针 知识点 1,数组就是指针,对应代码里的test1 2,用auto声明,得到的是指针,对应代码里的test2 3,用decltype声明,得到的不是指针 ,对应代码里的test3 ...

  9. 思科4506E做ehterchannel故障排查

    思科4506E做ehterchannel故障排查 转载于:https://blog.51cto.com/eric1026/1910912 一.故障描述 某客户有两台4506E汇聚交换机,需要做ethe ...

  10. git 使用命令删除远程分支和本地分支

    删除远程分支命令: git push origin   :<远程分支名称> git push origin --delete <远程分支名称> 删除本地分支: git bran ...