python3 写文件问题

0x92 即 10010010,UTF8 中编码一个字符的第一个字节(start byte)只可能是 0xxxxxxx110xxxxx1110xxx11110xxx……而后面的字节只可能是 10xxxxxx。也就是说 0x92 只能作为后面的字节,却出现在了第一个字节的位置。

出现这种问题绝大部分情况是因为文件不是 UTF8 编码的(例如,可能是 GBK 编码的),而系统默认采用 UTF8 解码。解决方法是改为对应的解码方式。

极少数情况是因为文件损坏了或者和一部分非 UTF8 编码混在一起,可以修复文件或采用 replace 等方式解码。

Python 2 没有问题是因为 Python 2 默认以字节流(对应 Python 3 的 bytes)的方式读文件,不像 Python 3 默认解码为 unicode,把读文件的方式改为 rb 有同样的效果,不过 Python 3 中处理 bytes 还可能遇到很多问题就是了……

此种方式报错 UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xea in position 3171: invalid c

with open(childrenFile + "\\" + childrenFile_00,"r", "utf-8") as f:
则尝试下面这种方式,解释如上with open(childrenFile + "\\" + childrenFile_00,"rb") as f:

python3 读取文件 写入库/*******************************************************************************************************/
#coding:utf-8

from imp import reload

import pymysqlimport timeimport jsonimport datetimeimport requestsimport urllibimport datetimeimport getoptimport randomimport xlwtimport osimport sysimport codecsreload(sys)

if os.path.exists(‘logs‘)==False:    os.mkdir(‘logs‘)

config={‘host‘:‘127.0.0.1‘,       ‘user‘:‘root‘,       ‘password‘:‘root‘,       ‘port‘:3306 ,       ‘database‘:‘test‘,       ‘charset‘:‘utf8‘       }conn= pymysql.connect(**config)cur=conn.cursor()

path = ‘F:\BreachCompilation\data‘files = os.listdir(path)  

for file in files :   childrenFile  = path + "\\" + file   files_00 = os.listdir(childrenFile)

for childrenFile_00 in files_00 :

with open(childrenFile + "\\" + childrenFile_00,"rb") as f:         ftextlist = f.readlines()         start = 0         for line in ftextlist:            ##content_0  = ftextlist.split("\n")            element = line.decode("utf-8").split(":")            start = start + 1            print("-------索引------" + str(start))            if( len(element) >= 2 ):               print("邮箱" + element[0] + "密码:" + element[1])               insertSQL = "insert into 40G (uname, pwd) value(‘%s‘, ‘%s‘)" % (pymysql.escape_string(element[0]), pymysql.escape_string(element[1]))               cur.execute(insertSQL)               conn.commit()

##element = content.split(":")         ##print(element[0] + "----" + element[1])         ##insertSQL = "insert into 40G (uname, pwd) value(‘%s‘, ‘%s‘)" % (element[0], element[1])

print("over")##cur.close()##conn.close()

/**********************python3 当 \x 需要转义  转义为 \/x**************************************************************************/
#coding:utf-8

from imp import reload

import pymysqlimport timeimport jsonimport datetimeimport requestsimport urllibimport datetimeimport getoptimport randomimport xlwtimport osimport sysimport codecsreload(sys)

if os.path.exists(‘logs‘)==False:    os.mkdir(‘logs‘)

config={‘host‘:‘127.0.0.1‘,       ‘user‘:‘root‘,       ‘password‘:‘root‘,       ‘port‘:3306 ,       ‘database‘:‘test‘,       ‘charset‘:‘utf8‘       }conn= pymysql.connect(**config)cur=conn.cursor()

path = ‘E:\/xunleixiazai\BreachCompilation\data‘files = os.listdir(path)  

for file in files :   childrenFile  = path + "\\" + file   files_00 = os.listdir(childrenFile)

for childrenFile_00 in files_00 :

with open(childrenFile + "\\" + childrenFile_00,"rb") as f:         ftextlist = f.readlines()         start = 0         for line in ftextlist:            ##content_0  = ftextlist.split("\n")            element = line.decode("utf-8").split(":")            start = start + 1            print("-------索引------" + str(start))            if( len(element) >= 2 ):               print("邮箱" + element[0] + "密码:" + element[1])               insertSQL = "replace into 40G (uname, pwd) value(‘%s‘, ‘%s‘)" % (pymysql.escape_string(element[0]), pymysql.escape_string(element[1]))               cur.execute(insertSQL)               conn.commit()

##element = content.split(":")         ##print(element[0] + "----" + element[1])         ##insertSQL = "insert into 40G (uname, pwd) value(‘%s‘, ‘%s‘)" % (element[0], element[1])

print("over")##cur.close()##conn.close()




原文地址:https://www.cnblogs.com/he0xff/p/8560703.html

时间: 2024-08-13 04:10:13

python3 写文件问题的相关文章

python3 写文件

#coding:utf-8 wfile = open('test.txt','w') str=''' 在广大玩家的要求下这款修改器诞生 了,这首先要归功于太阁狂人_Cath_Sean 的大力支持,这款修改器80%的数据都是有 他提供的.真的是非常的感谢!然后还要 感谢光荣论坛的每一位,他们都给了我很 大的帮助,尤其是版主浪子韩柏亲自测试 了此修改器的每一个版本. 关于游戏中一些上限的修改请期待 TK4Perfect-MermoyCheat版 游侠网 清水剑神 2002年04月04日''' for

Python2.X和Python3.X文件对话框、下拉列表的不同

Python2.X和Python3.X文件对话框.下拉列表的不同 今天初次使用Python Tkinter来做了个简单的记事本程序.发现Python2.x和Python3.x的Tkinter模块的好多内置函数都有所改变,这里简单整理一下以备日后查验. 一.导入方式: Python2.x: from Tkinter import * Python3.x: from tkinter import * 二.打开文件框: Python2.X: import tkFileDialog filename =

Python3 对文件操作

计算机文件 在计算机系统中,以硬盘为载体存储在计算机上的信息集合称为文件.文件可以是文本文档.图片.声音.程序等多种类型.在编程时经常要对文件进行读写等操作, 从程序员的视角可以把文件理解为是连续的字节序列,进行数据传输需要使用字节流, 字节流可以是由单个字节或大块数据组成.文件类型通常分为文本文件和二进制文件. 文件操作 在 Python中对文件进行操作分为3个步骤,首先要打开文件,然后是对文件进行读写操作,最后需要关闭文件. 打开文件---open函数 你必须先用Python内置的open(

cocos2d-x3.0 用CCDictionary写文件

bool CDownLoad_LocalData::WriteToConfigFile( DownLoadLocalData* downdata ){ CCDictionary* pDict = CCDictionary::create(); unsigned int lessonid = downdata->lession_id;char s_lessonid[10];sprintf(s_lessonid,"%d",lessonid); CCString* pValue1 =

java创建文本、文件、读文件、写文件

1 package Head18; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 10 public class Mkdirs_FileRW { 11 public st

笨方法学python--读写文件

1 文件相关的函数 close read readline  读取文本文件中的一行 truncate  清空文件 write('adb') 写入 2 写文件,首先要在open时,写入权限w target = open(filename, 'w') 当使用w权限时,即使不使用target.truncate(),后面write后也是代替原内容,相当于先truncate了 若是使用a权限,可以在文件末尾追加新write内容. target = open(filename, 'a') 若不写权限,相当于

使用 Java 程序写文件时,记得要 flush()

使用 Java 程序往磁盘写文件时碰到了这样的问题:文件写不全. 假如内容(StringBuffer/StringBuilder)有 100W 个字符,但是通过 Java 程序写到文件里的却不到 100W ,部分字符不见了. 代码大致是这样的: 1 private void writeToDisk() throws Exception { 2 File file = new File("FILE_PATH"); 3 OutputStreamWriter osw = null; 4 os

python3写的腾讯漫画下载器

代码很稀烂,开坑后一个月的时间,断断续续总算是写完了,主体功能完成,顺便PYQT5写了个GUI,并用cx_freeze打包,可以在windows下用. 项目托管在github: https://github.com/abcfy2/getComic 预览效果 放几张预览图,支持不连续的章节选择下载. windows下的效果: deepin下效果: 算法描述 腾讯的漫画从PC版访问时看到的是flash,但是移动版的页面却是图片,用ipad的UA请求ac.qq.com可以发现跳转为m.ac.qq.co

java中IO写文件工具类

下面是一些根据常用java类进行组装的对文件进行操作的类,平时,我更喜欢使用Jodd.io中提供的一些对文件的操作类,里面的方法写的简单易懂. 其中jodd中提供的JavaUtil类中提供的方法足够我们使用,里面的方法写的非常简练,例如append,read等方法,封装更好,更符合面向对象, 这里面我写的一些方法可多都是模仿jodd,从里面进行抽取出来的. /** * 获取路径文件夹下的所有文件 * @param path * @return */ public static File[] ge