python3大文件解压和基本操作
先说下:所谓的大文件并不是压缩文件有多大,几十兆的文件而是解压后几百兆。其中就遇到解压不成功的情况.、读小文件时成功,大文件时失败等
def unzip_to_txt_plus(zipfilename): zfile = zipfile.ZipFile(zipfilename, 'r') for filename in zfile.namelist(): data = zfile.read(filename) # data = data.decode('gbk').encode('utf-8') data = data.decode('gbk', 'ignore').encode('utf-8') file = open(filename, 'w+b') file.write(data) file.close() if __name__ == '__main__': zipfilename = "E:\\share\\python_excel\\zip_to_database\\20171025.zip" unzip_to_txt_plus(zipfilename)
注意参数:‘ignore' ,因为默认是严格编码,如果不加这个参数就会报错。
因为该函数已经把文件编成utf-8 所以后面读取文件时成功,下面贴出读取大文件代码(忽略数据库相关)
# - coding: utf-8 - import csv import linecache import xlrd import MySQLdb def txt_todatabase(filename, linenum): # with open(filename, "r", encoding="gbk") as csvfile: # Read = csv.reader(csvfile) # count =0 # for i in Read: # # print(i) # count += 1 # # print('hello') # print(count) count = linecache.getline(filename, linenum) print(count) # with open("new20171028.csv", "w", newline="") as datacsv: # # dialect为打开csv文件的方式,默认是excel,delimiter="\t"参数指写入的时候的分隔符 # csvwriter = csv.writer(datacsv, dialect=("excel")) # # csv文件插入一行数据,把下面列表中的每一项放入一个单元格(可以用循环插入多行) # csvwriter.writerow(["A", "B", "C", "D"]) def bigtxt_read(filename): with open(filename, 'r', encoding='utf-8') as data: count =0 while 1: count += 1 line = data.readline() if 1000000 == count: print(line) if not line: break print(count) if __name__ == '__main__': filename = '20171025.txt' txt_todatabase(filename, 1000000) bigtxt_read(filename)
经过对比,发现两个速度基本一样快。两百万行的数据是没压力的。
相关推荐
chuckchen 2020-10-31
Will0 2020-10-12
Dreamhome 2020-10-09
xirongxudlut 2020-09-28
星辰大海的路上 2020-09-13
chaochao 2020-08-31
猪猪侠喜欢躲猫猫 2020-08-17
快递小可 2020-08-16
shengge0 2020-07-26
巩庆奎 2020-07-21
张文倩数据库学生 2020-07-19
xirongxudlut 2020-07-18
Ericbig 2020-07-18
kyelu 2020-07-09
liangzhouqu 2020-07-07
GuoSir 2020-06-28
chaigang 2020-06-27
pythonxuexi 2020-06-25