简体   繁体   中英

Migrating from Python 2 to Python 3

I am trying to migrate project from Python 2 to Python 3. For this I used 2to3.exe. It made most of the things correct.

But my real problem is :

1. Data is stored by Python 2 Code. This data is very huge and I can't change it.
2. While reading this data in Python 3 getting error for marshalling and text data. 

For text can we use decode function using latin-1 ?

Marshal eg :

Python 2 code

import marshal
ad = {'a': 2319, 'b': '1', 'c': '1'}
marshalled_data = marshal.dumps(ad)
#'{t\x01\x00\x00\x00ai\x0f\t\x00\x00t\x01\x00\x00\x00ct\x01\x00\x00\x001t\x01\x00\x00\x00bR\x02\x00\x00\x000'

Python 3 code

import marshal
marshalled_data = b'{t\x01\x00\x00\x00ai\x0f\t\x00\x00t\x01\x00\x00\x00ct\x01\x00\x00\x001t\x01\x00\x00\x00bR\x02\x00\x00\x000'
ad = marshal.loads(marshalled_data)

Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
ValueError: bad marshal data (unknown type code)

Currently I don't see any option to have marshalling reading python 2 data in Python 3 code base. Has anyone worked on it?

It will be very helpful if any pointer is provided for further investigations.

Today I got solution for marshalling is as below:

from pwnypack import marshal
atext = b'{t\x01\x00\x00\x00ai\x0f\t\x00\x00t\x01\x00\x00\x00ct\x01\x00\x00\x001t\x01\x00\x00\x00bR\x02\x00\x00\x000'
marshal.marshal_loads(atext, origin={'version': 27})

atext is string generated by marshal.dumps of Python2.7 version

Don't know any other implications in it. But it solved problem mentioned in question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM