将目录中具有多种编码的所有文本文件转换为utf8编码的文本文件

2024-05-16 04:42:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的新手,一般来说,也是编码方面的新手。因此,我们非常感谢您的帮助

我在一个目录中有3000多个文本文件,有多种编码。我需要将它们转换为单个编码(例如utf8),以便进行进一步的NLP工作。当我使用shell检查这些文件的类型时,我确定了以下编码:

Algol 68 source text, ISO-8859 text, with very long lines
Algol 68 source text, Little-endian UTF-16 Unicode text, with very long lines
Algol 68 source text, Non-ISO extended-ASCII text, with very long lines
Algol 68 source text, Non-ISO extended-ASCII text, with very long lines, with LF, NEL line terminators
ASCII text
ASCII text, with very long lines
data
diff output text, ASCII text
ISO-8859 text, with very long lines
ISO-8859 text, with very long lines, with LF, NEL line terminators
Little-endian UTF-16 Unicode text, with very long lines
Non-ISO extended-ASCII text
Non-ISO extended-ASCII text, with very long lines
Non-ISO extended-ASCII text, with very long lines, with LF, NEL line terminators
UTF-8 Unicode (with BOM) text, with CRLF line terminators
UTF-8 Unicode (with BOM) text, with very long lines, with CRLF line terminators
UTF-8 Unicode text, with very long lines, with CRLF line terminators

如何将具有上述编码的文本文件转换为具有utf-8编码的文本文件


Tags: textextended编码withlineasciiunicodeiso
1条回答
网友
1楼 · 发布于 2024-05-16 04:42:10

我遇到了和你一样的问题。 我用了两个步骤来解决这个问题

代码如下:

import os, sys, codecs
import chardet

首先,使用chardet包识别文本的编码

for text in os.listdir(path):
    txtPATH = os.path.join(path, text)
    txtPATH=str(txtPATH)
    

    f = open(txtPATH, 'rb')
    data = f.read()
    f_charInfo = chardet.detect(data)
    coding2=f_charInfo['encoding']
    coding=str(coding2)
    print(coding)
    data = f.read()

其次,如果文本编码不是utf-8,则将文本重写为utf-8编码到目录中

        if not re.match(r'.*\.utf-8$', coding, re.IGNORECASE): 
        print(txtPATH)
        print(coding)

        with codecs.open(txtPATH, "r", coding) as sourceFile:
            contents = sourceFile.read()
            
            
            with codecs.open(txtPATH, "w", "utf-8") as targetFile:              
                targetFile.write(contents)

希望这能有所帮助!谢谢

相关问题 更多 >