Why don't you read the file and write it as UTF-8? You can do that in Python.

#to support encodings
import codecs

#read input file
with codecs.open(path, 'r', encoding = 'utf8') as file:
  lines = file.read()

#write output file
with codecs.open(path, 'w', encoding = 'utf8') as file:
  file.write(lines)
Answer from 3Ducker on Stack Overflow
🌐
Notepad++ Community
community.notepad-plus-plus.org › topic › 24214 › python-multiple-files-ansi-to-utf-8-converter
Python: Multiple files ANSI to utf-8 converter | Notepad++ Community
March 7, 2023 - ''' with open(fname, encoding=from_encoding) as f: text = f.read() with open(fname, 'w', encoding=to_encoding) as f: f.write(text) if __name__ == '__main__': import argparse import glob parser = argparse.ArgumentParser() parser.add_argument('dirname', help='d:\\2022_12_02\\word 2\\1') # name of directory in which you want to change file encodings parser.add_argument('old_encoding', help='ANSI') # the previous encoding of files found parser.add_argument('new_encoding', nargs='?', default='utf-8', help='UTF-8') parser.add_argument('include_files', nargs='*', help='*.txt') # filename patterns usi
Discussions

encoding - convert ansi escape to utf-8 in python - Stack Overflow
I may be wrong in accessing weather this string is ansi or anything else but it comes from rtf docs with heading. {\rtf1\ansi\ansicpg1252 the string of interest from doc is: ansi_string = r'3 \u... More on stackoverflow.com
🌐 stackoverflow.com
Python 3 ANSI to UTF-8
I'm a complete Python noob so any assistance would be greatly appreciated. I've been working with .zip files contained within multiple… More on reddit.com
🌐 r/learnpython
1
3
May 30, 2018
UTF-8 and ANSI encoding issue
Hi all, i have a code that writes to a file with utf-8 encoding. But when i try to open the same file i created in the same script i get an error message saying it can’t read it because the file is in ANSI. Here is the code of creating the file: with open(new_file, 'w', encoding='utf-8') ... More on discuss.python.org
🌐 discuss.python.org
10
0
November 23, 2023
python - From ansi encoding to utf8 (and hex bytes) - Stack Overflow
I have some texts encoded in ansi windows codepage. It is known which codepage it is. The data is stored in text files. I would like to do the following: convert the to utf-8 print the resulting u... More on stackoverflow.com
🌐 stackoverflow.com
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 318059 › how-to-change-text-file-encoding-with-python
tkinter - How to change text file encoding with python? [SOLVED] | DaniWeb
UTF-8 does not have a header by default, but some Windows tools historically detect UTF-8 only when a BOM is present. In that case, use encoding='utf-8-sig' when opening the file; Python will write the BOM automatically at the start of the file ...
🌐
CodingTechRoom
codingtechroom.com › question › convert-ansi-to-utf8
How to Programmatically Convert an ANSI Text File to UTF-8 Encoding - CodingTechRoom
# Python Code to Convert ANSI to UTF-8 with open('file_ansi.txt', 'r', encoding='mbcs') as ansi_file: content = ansi_file.read() with open('file_utf8.txt', 'w', encoding='utf-8') as utf8_file: utf8_file.write(content)
🌐
Stack Overflow
stackoverflow.com › questions › 42550137 › convert-ansi-escape-to-utf-8-in-python
encoding - convert ansi escape to utf-8 in python - Stack Overflow
I may be wrong in accessing weather this string is ansi or anything else but it comes from rtf docs with heading. {\rtf1\ansi\ansicpg1252 the string of interest from doc is: ansi_string = r'3 \u...
🌐
GitHub
gist.github.com › dogancelik › 2a88c81d309a753cecd8b8460d3098bc
ANSI / UTF-8 (with or without BOM) conversion #Windows · GitHub
from glob import glob from Npp import notepad globPath = "C:\MyFiles\*.txt" for file in glob(globPath): notepad.open(file) notepad.runMenuCommand("Encoding", "Convert to UTF-8-BOM") notepad.save() notepad.close()
🌐
Reddit
reddit.com › r/learnpython › python 3 ansi to utf-8
r/learnpython on Reddit: Python 3 ANSI to UTF-8
May 30, 2018 - When I open the files in Notepad then manually change the encoding to UTF-8, from ANSI, Splunk will ingest the files properly. - I'm looking for a way to automate this process, as drilling down and changing the encoding on hundreds of files in dozens of directories is a bit tedious. Share ... What was the first Python project that made you feel like: “okay…
Find elsewhere
🌐
Python.org
discuss.python.org › python help
UTF-8 and ANSI encoding issue - Python Help - Discussions on Python.org
November 23, 2023 - But when i try to open the same file i created in the same script i get an error message saying it can’t read it because the file is in ANSI. Here is the code of creating the file: with open(new_file, 'w', encoding='utf-8') ...
🌐
PyPI
pypi.org › project › ansipants
ansipants · PyPI
A Python module and command-line utility for converting .ANS format ANSI art to HTML. ... For additional options, run python -m ansipants --help. The output is a fragment of HTML, in UTF-8 encoding, intended to be inserted into a preformatted text element (<pre>...</pre>).
      » pip install ansipants
    
Published: Dec 25, 2021
Version: 0.2
🌐
Stack Overflow
stackoverflow.com › questions › 75466349 › from-ansi-encoding-to-utf8-and-hex-bytes
python - From ansi encoding to utf8 (and hex bytes) - Stack Overflow
I have some texts encoded in ansi windows codepage. It is known which codepage it is. The data is stored in text files. ... Did read python encoding guide, but I could not get the answer. ... import codecs chinaAnsi = '\xCE\xD2' # 我 in chinese GBK CJK Unified Ideograph-6211 # 0xE6 0x88 0x91 in UTF8 print(chinaAnsi.encode('utf-8').decode('utf-8')) # results in b'\xc3\x8e\xc3\x92' or ÎÒ # which is meaningless.
Top answer
1 of 2
1

You can try this code:

import codecs
import os
import sys

filePathSrc="C:\\222\\3" # Path to the folder with files to convert
for root, dirs, files in os.walk(unicode(filePathSrc)):
    for fn in files: 
        if fn[-4:] == '.srt': # Specify type of the files
            filename = unicode(root + "\\" + fn)
            with codecs.open(filename,'r', encoding = "Windows-1251") as f:
                text = f.read()
                # process Unicode text
            with codecs.open(filename,'w',encoding='utf8') as f:
                # f.write(u'\uFEFF') # BOM mark optional
                f.write(text)

Points:

  • import codecs added to work with files in Python
  • os.walk(unicode(filePathSrc)) is given a Unicode path to return Unicode file names
  • You should specify the correct encoding for your files instead of Windows-1251 in the with codecs.open(filename,'r', encoding = "Windows-1251") code.
  • If the folder filePathSrc variable should have Unicode chars, convert them to \uXXXX notation (you can do that easily with r12a Unicode Converter from the JavaScript escapes field). Say, your folder name is 7 Minutes 2014{جنایی}{7 دقیقه}. You paste it to the green field, and click Convert. Then, grab the string from the JavaScript escapes field and use it for filePathSrc variable while also pre-pending the string with u"" prefix. It will look as filePathSrc=u"c:\\222\\7 Minutes 2014{\u062C\u0646\u0627\u06CC\u06CC}{7 \u062F\u0642\u06CC\u0642\u0647}". Then, instead of os.walk(unicode(filePathSrc)) use os.walk(filePathSrc) since the string we pass is already Unicode.
2 of 2
0

If you use a Unicode path in os.walk() it will return Unicode paths and filenames. Notepad isn't required to convert the files. Below is code that will work in Python 2 and Python 3 since it wasn't specified.

Note that strings are Unicode by default in Python 3, but the from __future__ makes Python 2 strings default Unicode where normally they are byte strings. Making sure you use Unicode strings everywhere is important.

io.open is the Python 3 version of open, but is available in Python 2 as well. It opens files with "ANSI" encoding by default. locale.getpreferredencoding() can be used to determine the exact encoding. It is cp1252 on US Windows. read() will return the file data decoded into Unicode.

The encoding utf-8-sig will prepend a UTF-8-encoded BOM character (which Windows tends to like) and encode the written data using UTF-8. If the BOM is not desired, use utf8 instead.

from __future__ import unicode_literals
import os
import io
import fnmatch
filePathSrc = r'C:\test'
for root, dirs, files in os.walk(filePathSrc):
    for fn in fnmatch.filter(files,'*.srt'):
        fullname = os.path.join(root,fn)
        with io.open(fullname) as f:
            data = f.read()
        with io.open(fullname,'w',encoding='utf-8-sig') as f:
            f.write(data)
🌐
Roger Pearse
roger-pearse.com › weblog › 2021 › 05 › 14 › converting-old-html-from-ansi-to-utf-8-unicode
Converting old HTML from ANSI to UTF-8 Unicode
May 15, 2021 - There is a way to efficiently convert your masses of ANSI files to UTF-8, and I owe my knowledge of it to this StackExchange article here. You do it in Notepad++. You can write a macro that will run the editor and just do it. It runs very fast, it is very simple, and it works. You install the “Python Script” plugin into Notepad++ that allows you to run a python script.
🌐
Example Code
example-code.com › python › charset_convert_file_from_utf8_to_ansi.asp
CkPython Convert a File from utf-8 to ANSI (such as Windows-1252)
Chilkat • HOME • Android™ • AutoIt • C • C# • C++ • Chilkat2-Python • CkPython • Classic ASP • DataFlex • Delphi DLL • Go • Java • Node.js • Objective-C • PHP Extension • Perl • PowerBuilder • PowerShell • PureBasic • Ruby • SQL Server • Swift • Tcl • Unicode C • Unicode C++ • VB.NET • VBScript • Visual Basic 6.0 • Visual FoxPro • Xojo Plugin
🌐
Dynamo
forum.dynamobim.com › developers
Convert multiple text files from Unicode to ANSI - Developers - Dynamo
September 25, 2017 - Hi, I have used the “export schedule” Dynamo script shared in this website to export my Revit schedules to text files. However, I would like expand the script to further convert the text files format from Unicode to ANSI with Python script. Is it possible?
🌐
Chilkat
chilkatsoft.com › python_strings.asp
Strings in Python: UTF-8 and Source File Encoding
Always save Python files as UTF-8 in editors like VS Code, PyCharm, or Notepad++. Specify encoding explicitly if needed (for compatibility with older Python versions): ... s = "Café" b = s.encode("utf-8") # Convert to bytes print(b) # Output: b'Caf\xc3\xa9' s2 = b.decode("utf-8") # Convert ...