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
  • Solved that one.
    Needed to be re.sub(b"\r\n", b"\n", data)
     
    My next task:
    Accessing the acar.log with
    f=open(FileName)
    lines=f.readlines()
     
    throws
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "<string>", line 25, in <module>
    File "/appl/mdi/2018_RH6/python/linux64/lib/python3.5/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 6951: ordinal not in range(128)
    ! &> integer = (eval(run_python_code("exec(open('grep_file.py').read())")))
     
Reply
  • Solved that one.
    Needed to be re.sub(b"\r\n", b"\n", data)
     
    My next task:
    Accessing the acar.log with
    f=open(FileName)
    lines=f.readlines()
     
    throws
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "<string>", line 25, in <module>
    File "/appl/mdi/2018_RH6/python/linux64/lib/python3.5/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 6951: ordinal not in range(128)
    ! &> integer = (eval(run_python_code("exec(open('grep_file.py').read())")))
     
Children
No Data