hexagon logo

Converting code to Python 3

Ho folks,
I'm about to check our code for future usage with ADAMS 2018 which uses Python 3 . (It's working as is in Python 2.x)
 
This code that's used to do a dos2unix on Windoze machines throws errors that I can't resolve:
 
import os, sys, string
import re
 
# In_File = File to be converted
In_File = 'test.txt'
# -----------------------------------
# Convert to unix
# -----------------------------------
data = open(In_File, "rb").read()
newdata = re.sub("\r\n", "\n", data)
if newdata != data:
   print('   ' + In_File + " \tis converted to unix format ...")
   f = open(In_File, "wb")
   f.write(newdata)
   f.close()
 
Any proposals for replacing
re.sub("\r\n", "\n", data)
that may work ?
 
Parents
  • Martin,
     
    The 'open' function in Py3 is fully aware of the native context, so it will "do the right thing" on any given platform. You can force it on write, though, as I did with the example, to write Unix eol (linefeed) irrespective of platform. You could just as easily force DOS with "\r\n", or you could leave off the newline parameter and just let it write native ones.
     
    Yes, there are a lot of little things changed in 3.x, but they are pretty easily dealt with and the overall improvement to the language is huge (especially for anyone who ever had to deal with non-ASCII text, say, maybe something with umlauts or other diacritics in it). In Py2, the Unicode handling was spotty and inconsistent, which ended up corrupting things and forcing you into various tricks that didn't always work. Py3 fixes all that by putting the encode/decode firewall between strings and bytes. When they made that fix, they had the opportunity to clean things up, so they took it, no more string module methods, they are all on string now.
     
    > alias 2to3  '"t:/Python27/Tools/Scripts/2to3.py" --fix=all --fix=idioms --nofix=basestring \!:*'
     
    Overall, Py3 is greatly improved compared to Py2. In 3.4 (the first "useable" version of 3 in my opinion), you were about at status quo with 2.7, but now with 3.6, it's way faster, has a smaller memory footprint, much richer in both the libraries and language constructs, and has all sorts of nice little tweaks to make your code much more readable. Here are a couple:
     
    Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49)
    >>> x = 1_000_000
    >>> y = f"The value of x is {x}"
    >>> y
    'The value of x is 1000000'
     
Reply
  • Martin,
     
    The 'open' function in Py3 is fully aware of the native context, so it will "do the right thing" on any given platform. You can force it on write, though, as I did with the example, to write Unix eol (linefeed) irrespective of platform. You could just as easily force DOS with "\r\n", or you could leave off the newline parameter and just let it write native ones.
     
    Yes, there are a lot of little things changed in 3.x, but they are pretty easily dealt with and the overall improvement to the language is huge (especially for anyone who ever had to deal with non-ASCII text, say, maybe something with umlauts or other diacritics in it). In Py2, the Unicode handling was spotty and inconsistent, which ended up corrupting things and forcing you into various tricks that didn't always work. Py3 fixes all that by putting the encode/decode firewall between strings and bytes. When they made that fix, they had the opportunity to clean things up, so they took it, no more string module methods, they are all on string now.
     
    > alias 2to3  '"t:/Python27/Tools/Scripts/2to3.py" --fix=all --fix=idioms --nofix=basestring \!:*'
     
    Overall, Py3 is greatly improved compared to Py2. In 3.4 (the first "useable" version of 3 in my opinion), you were about at status quo with 2.7, but now with 3.6, it's way faster, has a smaller memory footprint, much richer in both the libraries and language constructs, and has all sorts of nice little tweaks to make your code much more readable. Here are a couple:
     
    Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49)
    >>> x = 1_000_000
    >>> y = f"The value of x is {x}"
    >>> y
    'The value of x is 1000000'
     
Children
No Data