Hi, i can't make this work. I use your code to read my file, works:
Mark all code an push "Remove Formatting" button next time.
# read in the file contents iso = open('E:/ENEL/Modelos/NW201610/DGER.dat').read() utf = open('E:/ENEL/Modelos/NW201610/DGER.dat').read() # this is how they look, one <str> character for each byte in the source file print 'ISO:', repr(iso) print 'UTF:', repr(utf) # transform them to unicode, specifying the appropriate encoding unicodeISO = unicode(iso, encoding='iso-8859-15') #unicodeUTF = unicode(utf, encoding='UTF-8') # Now, as unicode strings, they are identical print repr(unicodeISO), unicodeISO #print repr(unicodeUTF), unicodeUTFBut my original function does not work:
def dger_ano(FILE_ORIGEM, FILE_DATE, ORIGEM, DESTINO): #ORIGEM: ../file.txt | DESTINO: ../../ cache = None string = "YEAR " + FILE_ORIGEM with open(ORIGEM , "r") as f: cache = f.read() unicodeISO = unicode(cache, encoding='iso-8859-15') print ('ISO:', repr(unicodeISO)) new_file = re.sub(string, "ANO INICIO DO ESTUDO {}".format(FILE_DATE), cache) if new_file: with open(DESTINO + "/temporario.txt", "w") as unicodeISO: unicodeISO.write(new_file) os.remove(DESTINO + "/DGER.dat") os.rename(DESTINO + '/temporario.txt',DESTINO + '/DGER.dat')Edit adim:
Mark all code an push "Remove Formatting" button next time.