cPickle EOF Error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roopesh

    #1

    cPickle EOF Error

    I am trying to write xml files which are inside a zip file into the
    database.

    In the zipfile module the function read returns bytes. What I did was
    to make a blob out of the returned bytes and write it to the
    database(for this I used cPickle). I also used _mysql's escape_string
    which escapes some characters (without this the file content was not
    getting written). But once we try to unpickle the content, an EOF
    error is getting thrown. When I tried putting the content(xml in
    database) to a file without unpickling then the xml file with escape
    characters gets printed.

    My code is as follows :

    def import_cards():
    try :
    conn =
    MySQLdb.connect (host="localhos t",port=3306,us er="roopesh",pa sswd="pass",db= "mydbs")

    cursor = conn.cursor()

    cursor.execute( "CREATE TABLE card (serial_no int(10),
    id varchar(50), data blob, used char(1), ip varchar(20),
    date_downloaded datetime)")

    z = zipfile.ZipFile ('/home/roopesh/public_html/
    cards.zip', 'r')

    sql = "INSERT INTO card (serial_no, id, data, used)
    values (%s, %s, %s, %s)"


    for filename in z.namelist() :
    bytes = z.read(filename )
    data = cPickle.dumps(b ytes, 2)
    #print data
    cursor.execute( sql, (1, filename ,
    _mysql.escape_s tring(data), 'n'))

    sql = "SELECT serial_no, id, data, used FROM card"

    cursor.execute( sql)
    for serial_no, id, data, used in cursor.fetchall () :
    print serial_no, id,
    cPickle.loads(d ata.tostring())
    # f = open(id ,'wb')
    # f.write(data.to string())
    finally :
    cursor.execute( "DROP TABLE card")
    conn.close()

    return True

    The output is :
    --------------------
    roopesh@dg:~/public_html$ python cardManagement. py
    1 cards/passcard1.xml
    Traceback (most recent call last):
    File "cardManagement .py", line 136, in ?
    import_cards()
    File "cardManagement .py", line 28, in import_cards
    print serial_no, id, cPickle.loads(d ata.tostring())
    EOFError

Working...