1
  1. I have a .txt file in UTF-16-LE encoding .
  2. I want to remove the headers(1st row) and save it in ANSI
  3. I can do it maually but i need to do that for 150 txt files EVERY day
  4. So i wanted to use Python to do it automatically.

But i am stuck , i have tried this code but it is not working ,produces an error :

*"return mbcs_encode(input, self.errors)[0]

UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character "*

filename = "filetochangecodec.txt"
path = "C:/Users/fallen/Desktop/New folder/"
pathfile = path + filename
coding1 = "utf-16-le"
coding2 = "ANSI"

f= open(pathfile, 'r', encoding=coding1)
content= f.read()
f.close()
f= open(pathfile, 'w', encoding=coding2)
f.write(content)
f.close()

Fallen Greg
  • 23
  • 1
  • 7

1 Answers1

1

A noble contributer helped me with the solution and i now post it so everyone can benefit and save time. Instead of trying to write all the content , we make a list with every line of the txt file and then we write them in a new file one by one with the use of " for " .

import os

inpath = r"C:/Users/user/Desktop/insert/"
expath = r"C:/Users/user/Desktop/export/"
encoding1 = "utf-16"
encoding2 = "ansi"


 
input_filename = "text.txt"
input_pathfile = os.path.join(inpath, input_filename)

output_filename = "new_text.txt"
output_pathfile = os.path.join(expath, output_filename)



with open(input_pathfile, 'r', encoding=encoding1) as file_in:
    lines = []
    for line in file_in:
        lines.append(line)

with open(output_pathfile, 'w', encoding='ANSI') as f:
    for line in lines:
        f.write(line)
Fallen Greg
  • 23
  • 1
  • 7