简体   繁体   中英

How do I read python2 long integer syntax into python3?

I have an old file written using python2, which contains things like 148191387849281587952L . When I try to read it (using ast.literal_eval ) in python3, I, naturally, get SyntaxError: invalid syntax .

Is there anything I could do to read the file into python3?

PS. The only solutions I see at this time require modifying the file:

  1. Convert to JSON: read the file into python2, write it using json, then read into python3 using json.

  2. Remove the offending L using sed -i 's/([0-9]+)L/\\1/g' .

  3. Convert the file using 2to3 (suggested by @user3080953).

Is there anything better?

After your clarification, there is a simple answer:

No, you cannot modify ast.leteral_eval() or the python interpreter to be "backwards compatible" with the L syntax.

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