Python编写zlib压缩数据

时间:2016-01-29 12:39:36

标签: python file sorting compression zlib

我们开始之前使用的当前代码如下:

with open(classFileName,"a+"): as openFile
    fileData = openFile.readlines()
    try:
        fileData = zlib.decompress(str(fileData))
    except:
        pass

with open(classFileName,"a+") as openFile:
    openFile.write(fileData)

with open(classFileName,"a+") as openFile:
    fileData = []
    fileData = openFile.readlines()
    fileData.append(mergedData)

    fileData = sorted((a.strip().split() for a in fileData),key=operator.itemgetter(1))
    fileData = os.linesep.join(p[0] + ' ' + p[1] for p in fileData)

    fileData = str(fileData)
    zlib.compress(fileData)

with open(classFileName,"w") as openFile:
    openFile.write(fileData)

然而,这实际上会抛出一个错误,表示预期最后一行上的字符缓冲区对象。

该文件的示例如下所示:

Reece 5
约翰7 亚历克斯2

有任何建议或帮助吗?我想压缩以减少操纵分数。它看起来更好,因为我这样做并不担心原因。

1 个答案:

答案 0 :(得分:0)

Python中的字符串无法更改(字节对象也不能更改) - 因此,即使不查看文档也可以推断,CREATE TYPE Customer_obj AS OBJECT ( CUSTOMERID NUMBER, CUST_NAME NAME, CUST_ADDRESS ADDRESS_TYPE, CUST_CONTACT CONTACT_INFO, CUST_CREDITCARD CREDITCARD_INFO, CUST_PERSONALINFO PERSONAL_INFO ); CREATE TYPE NAME AS OBJECT ( FIRSTNAME VARCHAR2(80) , LASTNAME VARCHAR2(80) ); CREATE TYPE ADDRESS_TYPE AS OBJECT ( ADDRESS VARCHAR2(280) , CITY VARCHAR2(80) , STATE VARCHAR2(20), ZIP VARCHAR2(40) , COUNTRY VARCHAR2(40) , REGION VARCHAR2(40) ); CREATE TYPE CONTACT_INFO AS OBJECT ( EMAIL VARCHAR2(40), PHONE VARCHAR2(40) ); CREATE TYPE CREDITCARD_INFO AS OBJECT ( CREDITCARDTYPE VARCHAR2(40) , CREDITCARD VARCHAR2(40) , CREDITCARDEXPIRATION VARCHAR2(40) ); CREATE TYPE PERSONAL_INFO AS OBJECT ( USERNAME VARCHAR2(80) , PASSWORD VARCHAR2(80) , AGE NUMBER , INCOME NUMBER , GENDER VARCHAR(20) ); CREATE TABLE CUSTOMERS_NEW OF Customer_obj( PRIMARY KEY (CUSTOMERID)); CREATE TYPE Categories_obj AS Object( CATEGORY NUMBER , CATEGORYNAME VARCHAR2(80) ); CREATE TABLE CATEGORIES_NEW OF Categories_obj( PRIMARY KEY (CATEGORY)); CREATE TYPE Orders_obj AS Object( ORDERID NUMBER , ORDERDATE VARCHAR2(400), CUSTOMERID NUMBER, NETAMOUNT NUMBER , TAX NUMBER, TOTALAMOUNT NUMBER ); CREATE TABLE ORDERS_NEW OF Orders_obj( PRIMARY KEY (ORDERID), FOREIGN KEY(CUSTOMERID) REFERENCES CUSTOMERS_NEW ); CREATE TYPE Products_obj AS Object( PROD_ID NUMBER , CATEGORY NUMBER , TITLE VARCHAR2 (40) , ACTOR VARCHAR2 (40) , PRICE VARCHAR2 (40) ); CREATE TABLE PRODUCTS_NEW OF Products_obj( PRIMARY KEY (PROD_ID), FOREIGN KEY (CATEGORY) REFERENCES CATEGORIES_NEW ); CREATE TYPE Orderlines_obj AS Object( CUST_ID_INFO ID_INFO, QUANTITY NUMBER, ORDERDATE VARCHAR2 (80) ); CREATE TYPE ID_INFO AS Object ( ORDERLINEID NUMBER , ORDERID NUMBER , PROD_ID NUMBER ); CREATE TABLE ORDERLINES_NEW OF Orderlines_obj( PRIMARY KEY (CUST_ID_INFO.ORDERLINEID,CUST_ID_INFO.ORDERID), FOREIGN KEY (CUST_ID_INFO.ORDERID) REFERENCES ORDERS_NEW, FOREIGN KEY (CUST_ID_INFO.PROD_ID) REFERENCES PRODUCTS_NEW ); 无法修改您的zlib.compress。上面的代码片段只记录了原始的fileData。

来自the documentation

  

fileData{压缩字符串中的数据,   返回包含压缩数据的字符串。 level是一个整数   0到9控制压缩程度; 1是最快和生产   压缩程度最小,9最慢,产生最多。 0不是   压缩。默认值为6.如果有则引发错误异常   发生错误。

所以,

zlib.compress(string[, level])

应该适合你。